Sei sulla pagina 1di 185

Assignment No: 1 Statement: Write down the Matlab Program using Newton-Raphson method for any equation.

Solution: Input :
clc; clear all; % Clears the workspace format long; % Take i/p from user a=input('\n Enter the function : ','s'); b=input('\n Enter the derivative of function :

','s');

% Converts the input string into symbolic function ft=inline(a); dft=inline(b); n=input('enter no. of significant digits: '); t0=0; epsilon_s=(0.5*10^(2-n)); epsilon_a=100; tr=fzero((ft),t0); % Solver disp (tr); varun=sprintf('BY NEWTON-RAPHSON METHOD:'); disp(varun); tx=input('Enter your initial guess for root: '); td=tx; head=sprintf('Time \t\t\t\t\tepsilon_a \t\t\t\t\tepsilon_t \t\t\t\t\tepsilon_s '); disp(head); while (epsilon_a>=epsilon_s) tnew=td-(ft(td)/dft(td)); epsilon_a=abs((tnew-td)/tnew)*100; epsilon_t=abs((tr-tnew)/tr)*100; td=tnew; table=sprintf('%d \t\t\t %f\t\t\t\t %4.9f \t\t\t %f \t\t\t %f',tnew,epsilon_a,epsilon_t,epsilon_s); disp(table); end % Prints the answer fprintf('\n \n The root of the equation is : %f \n',tnew)

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Output :
Enter the function : (exp(t))*cos(t)-1.4

Enter the derivative of function : enter no. of significant digits: 4 0.433560875352657

(exp(t))*cos(t)-(exp(t))*sin(t)

BY NEWTON-RAPHSON METHOD: Enter your initial guess for root: 0 Time 4.000000e-001 epsilon_a 100.000000 epsilon_t 7.740752743 epsilon_s 0.005000

4.327044e-001

7.558146

0.197537266

0.005000

4.335602e-001

0.197392

0.000145353

0.005000

4.335609e-001

0.000145

0.000000000

0.005000

The root of the equation is :

0.433561

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 2 Statement: Write down the Matlab Program using Modified Newton-Raphson method for any equation.

Solution: Input :
clc; Clear all; % Clears the workspace % Takes the a=input('\n b=input('\n c=input('\n x0=0; i/p from user Enter the function : ','s'); Enter the derivative of function : ','s'); Enter second order derivative : ','s');

% Converts the input string into symbolic function fx=inline(a); dfx=inline(b); d2fx=inline(c); n=input('Enter number of significant digits: '); epsilon_s=(0.5*10^(2-n)); tr=fzero((fx),0); % Using solver disp (tr); v=input('\n Enter your initial guess for root : '); told=v; varun=sprintf('BY MODIFIED NEWTON-RAPHSON METHOD:'); disp(varun); head=sprintf('Time \t\t\t\t\tepsilon_a \t\t\t\t\tepsilon_t \t\t\t\t\tepsilon_s '); disp(head); while(1) tnew=told-((fx(told)*dfx(told)/((dfx(told)^2)-(fx(told)*d2fx(told))))); err=abs((tnew-told)/tnew)*100; epsilon_t=abs((tr-tnew)/tr)*100; told=tnew; table=sprintf('%d \t\t\t %f\t\t\t\t %4.9f \t\t\t %f \t\t\t %f',tnew,err,epsilon_t,epsilon_s); disp(table); if (err<=epsilon_s) break; end end fprintf('\n \n The root of the equation is : %f \n',tnew)

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Output
Enter the function : x*sin(x)+cos(x)

Enter the derivative of function :

x*cos(x)

Enter second order derivative :

cos(x)-(x*sin(x))

Enter number of significant digits: 5 -2.7984

Enter your initial guess for root : BY MODIFIED NEWTON-RAPHSON METHOD: Time 6.117645e+000 epsilon_a 1.923040

epsilon_t 318.613323399

epsilon_s 0.000500

6.121248e+000

0.058870

318.742096720

0.000500

6.121250e+000

0.000035

318.742173765

0.000500

The root of the equation is :

6.121250

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 3 Statement: Write down the Matlab Program using successive approximation method for any equation.

Solution: Input :
clc; clear; g=input('Enter the function:','s'); f=inline(g); % Defining function n=input('Enter number of significant digits: '); es=(0.5*10^(2-n)); % Stopping criteria ea=100; t0=0; t=input('Enter initial guess: '); tr=fzero((f),t0); % Calculating true roots disp (tr); head1=sprintf('BY SUCCESSIVE APPROXIMATION METHOD:'); disp(head1); head=sprintf('Time \t\t\t\t\tepsilon_a \t\t\t\t\t epsilon_t \t\t\t\t\t epsilon_s '); disp(head); while (ea>=es) temp=t; t=f(t); ea=abs((t-temp)/t)*100; % Calc approximate error et=abs((tr-t)/tr)*100; % Calc true error table=sprintf('%d \t\t\t %f\t\t\t\t %4.9f \t\t\t %f \t\t\t %f',t,ea,et,es); disp(table); end

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Output :
Enter the function:(exp(-x)-x) Enter number of significant digits: 2 Enter initial guess: 0 BY SUCCESSIVE APPROXIMATION METHOD: 0.567143290409784

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 4 Statement: Write down the Matlab Program using Gauss Nave elimination method.

Solution: Input :
clc; clear all; a=input('enter matrix A[]: ') b=input('enter column matrix B[]: ') [m,n]=size(a); % determines size of matrix. if (m~=n) error('Matrix Must Be Square!'); end
%forward elimination

for k=1:n-1 for i=k+1:n factor=a(i,k)/a(k,k); for j=k:n a(i,j)=a(i,j)-(factor*(a(k,j)));% calculates each element of matrix
A.

end b(i)=b(i)-factor*(b(k)); % calculates each element of matrix B. end disp (a); end disp (a); disp (b); % backward substitution for i=n:-1:1 x(i)=b(i)/a(i,i); % calculates values of unknown matrix. for j=1:i-1 b(j)=b(j)-x(i)*a(j,i); end end disp('VALUES ARE:') disp(x)

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Output :
enter matrix A[]: [1 -1 1 ;3 4 2 ; 2 1 1 ] a = 1 3 2 -1 4 1 1 2 1

enter column matrix B[]: [6 9 7] b = 6 9 7 1.0000 0 0 6.0000 -9.0000 -1.1429 VALUES ARE: 3.0000 -1.0000 2.0000 -1.0000 7.0000 0 1.0000 -1.0000 -0.5714

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 5 Statement: Write down the Matlab Program using Gauss with partial pivoting method.

Solution: Input :
clc; clear all; a=input('enter matrix A[]: '); b=input('enter column matrix B[]: '); [m,n]=size(a); % calculates size of matrix A. if (m~=n) error('Matrix Must Be Square!'); end
%pivoting

for k=1:n-1 [xyz,i]=max(abs(a(k:n,k))); % finds maximum element in matrix A. ipr=i+k-1; if ipr~=k a([k,ipr],:)=a([ipr,k],:); % interchanging of rows. b([k,ipr],:)=b([ipr,k],:); % interchanging of rows. end %forward elimination for i=k+1:n factor=a(i,k)/a(k,k); for j=k:n a(i,j)=a(i,j)-(factor*(a(k,j))); % calculates each element of matrix
A.

end b(i)=b(i)-factor*(b(k)); % calculates each element of matrix B. end disp (a); end %disp (a); disp (b); % backward substitution for i=n:-1:1 x(i)=b(i)/a(i,i); % calculates values of unknown matrix. for j=1:i-1 b(j)=b(j)-x(i)*a(j,i); end end disp('VALUES ARE:') disp(x)

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Output :
enter matrix A[]: [2 -6 -1;-3 -1 7;-8 1 -2] enter column matrix B[]: [-38;-34;-20] -8.00000000000000 0 0 1.00000000000000 -1.37500000000000 -5.75000000000000 -2.00000000000000 7.75000000000000 -1.50000000000000

-8.00000000000000 0 0

1.00000000000000 -5.75000000000000 0

-2.00000000000000 -1.50000000000000 8.10869565217391

-20.00000000000000 -43.00000000000000 -16.21739130434783

VALUES ARE: 4 8 -2

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 6 Statement: Write down the Matlab Program using Thomas Algorithm method.

Solution: Input :
clc; clear; %format long; e=input('Enter the value of e, ie. subdiagonal vector :'); f=input('Enter the value of f, ie. diagonal vector :'); g=input('Enter the value of g, ie. superdiagonal vector :'); r=input('Enter the value of r, ie. value vector :'); n=length(e); % Size of matrix e for k=1:n factor=e(k)/f(k); % Multiplying factor f(k+1)=f(k+1)-factor*g(k); % Transforming diagonal vector r(k+1)=r(k+1)-factor*r(k); % Transforming value vector end x(n+1)=r(n+1)/f(n+1); % Transforming unknown vector for k=n:-1:1 x(k)=(r(k)-g(k)*x(k+1))/f(k); % Finding values of unknowns end disp('VALUES ARE:'); disp (x)

Output :
Enter the value of e, ie. subdiagonal vector :[-.4;-.4] Enter the value of f, ie. diagonal vector :[0.8;0.8;0.8] Enter the value of g, ie. superdiagonal vector :[-.4;-.4] Enter the value of r, ie. value vector :[41;25;105] VALUES ARE: 173.7500 245.0000 253.7500

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 7 Statement: Write down the Matlab Program using Gauss Seidel without Relaxation method.

Solution: Input :
clc; clear all; format long; a = input('Enter Matrix A: '); b = input('Enter Column Matrix B: '); [m,n]= size(a); % calculates size of matrix A. if (m~=n) error('Matrix Must Be Square!'); end for i=1:n d(i)=b(i)/a(i,i); end d=d'; c=a; for i=1:n for j=1:n c(i,j)=a(i,j)/a(i,i); % factor. end c(i,i)=0; x(i)=0; end x=x'; disp (a); disp (b); disp (d); disp (c); p = input('Enter No. of Iterations: '); for k=1:p for i=1:n x(i)=d(i)-c(i,:)*x(:,1); % finds unknown value. end disp (x); end

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Output :
Enter Matrix A: [3 -0.1 -0.2;0.1 7 -.3;0.3 -0.2 10] Enter Column Matrix B: [7.85;-19.3;71.4] 3.00000000000000 0.10000000000000 0.30000000000000 -0.10000000000000 7.00000000000000 -0.20000000000000 -0.20000000000000 -0.30000000000000 10.00000000000000

7.85000000000000 -19.30000000000000 71.40000000000001

0 0.01428571428571 0.03000000000000

-0.03333333333333 0 -0.02000000000000

-0.06666666666667 -0.04285714285714 0

Enter No. of Iterations: 3 2.61666666666667 -2.79452380952381 7.00560952380952

2.99055650793651 -2.49962468480726 7.00029081106576

3.00003189791081 -2.49998799235305 6.99999928321562

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 8 Statement: Write down the Matlab Program using Gauss Seidel with relaxation method.

Solution: Input :
clc; clear all; a=input('enter matrix A[]: '); b=input('enter column matrix B[]: '); [m,n]=size(a); % calculates size of matrix A. if (m~=n) error('Matrix Must Be Square!'); end
%pivoting

for k=1:n-1 [xyz,i]=max(abs(a(k:n,k))); % finds maximum element in matrix A. ipr=i+k-1; if ipr~=k a([k,ipr],:)=a([ipr,k],:); % interchanging of rows. b([k,ipr],:)=b([ipr,k],:); % interchanging of rows. end end for i=1:n d(i)=b(i)/a(i,i); end d=d'; c=a; for i=1:n for j=1:n c(i,j)=a(i,j)/a(i,i); % factor. end c(i,i)=0; x(i)=0; end x=x'; disp (a); disp (b); disp (d); disp (c); lambda = input('Enter the value of weighting factor: '); es=0.05; % stopping criteria. ea(i)=100; head=sprintf('\t\t\t\t\t\t\t\t\tValue of x \t\t\t\t\t\t\t\t\t\t\t\t\t\tValue of ea '); disp(head); while (ea(i)>=es) for i=1:n y=x(i); x(i)=d(i)-c(i,:)*x(:,1); x(i)=lambda*x(i)+(1-lambda)*y; % calculates unknown value.

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

ea(i)=abs((x(i)-y)/x(i))*100; end table1=sprintf('%d \t\t\t %f\t\t\t\t %4.9f \t\t\t%f \t\t\t %f\t\t\t\t %4.9f',x,ea); disp(table1); end

Output :
enter matrix A[]: [-3 1 12;6 -1 -1;6 9 1] enter column matrix B[]: [50;3;40] 6 6 -3 -1 9 1 -1 1 12

3 40 50

0.5000 4.4444 4.1667

0 0.6667 -0.2500

-0.1667 0 0.0833

-0.1667 0.1111 0

Enter the value of weighting factor: 0.95

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Value of x Value of ea 4.750000e-001 3.921389 3.760702546 100.000000 100.000000 100.000000000 1.715081e+000 2.935111 1.709692e+000 2.830032 1.698338e+000 2.828267 4.321337313 4.356407772 4.355604413 72.304517 0.315233 0.668543 33.602766 3.712986 0.062401 12.973640471 0.805031598 0.018444248

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 42 Statement: Write down the Matlab Program to fit curve y = a0 + a1*x by using least square techniques for given set of points.

Solution: Input :
clc; clear all; x=input('Enter row matrix x : '); y=input('Enter row matrix y : '); [m,n]=size(x); xy(1,1)=0; i=1; X=0; Y=0; XY=0; Xsqr=0; while i<=n; xy(1,i)=x(1,i)*y(1,i); xsqr(1,i)=x(1,i)^2; X=X+x(1,i); % To calculate summation of x Y=Y+y(1,i); % To calculate summation of y XY=XY+xy(1,i); % To calculate summation of x*y Xsqr=Xsqr+xsqr(1,i); % To calculate summation of x^2 i=i+1; end disp(x); disp(y); a1=(n*XY-Y*X)/(n*Xsqr-X^2); a0=(Y*Xsqr-X*XY)/(n*Xsqr-X^2); ym=Y/n; sr(1,1)=0;j=1; while j<=n sr(1,j)=(y(1,j)-a0-a1*x(1,j))^2; % To calculate sr for each x st(1,j)=(y(1,j)-ym)^2; % To calculate st for each x j=j+1; end SR=sum(sr); ST=sum(st); r2=(ST-SR)/ST s=sprintf('Best fit curve (straight line) for above data is given by : y = %f * x + %f',a1,a0); disp(s); xp=linspace(min(x),max(x)); yp=a0+a1*xp; plot(x,y,'o',xp,yp); xlabel('values of x'); ylabel('values of y'); title('y=a0+a1*x');

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

grid on;

Output :
Enter row matrix x : [1.0 2.0 3.0 4.0 5.0 6.0 7.0] Enter row matrix y : [0.5 2.5 2.0 4.0 3.5 6.0 5.5] 1 2 3 4 5 6 7

0.5000 r2 = 0.8683

2.5000

2.0000

4.0000

3.5000

6.0000

5.5000

Best fit curve (straight line) for above data is given by : y = 0.839286 * x + 0.071429
y=a0+a1*x 6

values of y

4 values of x

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 9 Statement: Write down the Matlab Program to fit curve y = a0 + a1*x+a2x2 by using least square techniques for given set of points.

Solution: Input :
clc; clear all; x = input('Enter values of x in row matrix form : '); y = input('Enter values of y in row matrix form : '); [m,n]=size(x); sx = sum(x); sy = sum(y); sx2 = sum(x.*x); sxy = sum(x.*y); sx2y = sum(x.*x.*y); sx3 = sum(x.*x.*x); sx4 = sum(x.*x.*x.*x); a = [sx2 sx n; sx3 sx2 sx; sx4 sx3 sx2]; b = [sy; sxy; sx2y]; z=inv(a)*b; s=sprintf('Best fit curve (Quadratic) for above data is given by :y = %f + %f * x + %f * x^2 ',z(1),z(2),z(3)); disp(s); xp = linspace(min(x),max(x)); yp = z(3)*(xp.*xp)+z(2)*xp+z(1); plot(x,y,'o',xp,yp); grid on; xlabel('Values of x'); ylabel('Values of function'); title('y=a0+ a1*x+ a2*(x^2)');

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Output :
Enter values of x in row matrix form : [0.075 0.5 1 1.2 1.7 2.0 2.3] Enter values of y in row matrix form : [600 800 1200 1400 2050 2650 3750] Best fit curve (Quadratic) for above data is given by :y = 643.601494 + 218.884701 * x + 685.248397 * x^2

y=a0+ a1*x+ a2*(x 2) 4000

3500

3000

Values of function

2500

2000

1500

1000

500

0.5

1 Values of x

1.5

2.5

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 10 Statement: Write down the Matlab Program to fit curve y = a1*(xb1) by using least square techniques for given set of points.

Solution: Input :
clc; clear all; xa=input('Enter row matrix x : '); ya=input('Enter row matrix y : '); [m,n]=size(xa); xy(1,1)=0; y(1,1)=0; i=1; X=0; Y=0; XY=0; Xsqr=0; while (i<=n) y(1,i)=log10(ya(1,i)); x(1,i)=log10(xa(1,i)); xy(1,i)=x(1,i)*y(1,i); xsqr(1,i)=x(1,i)^2; X=X+x(1,i); Y=Y+y(1,i); XY=XY+xy(1,i); Xsqr=Xsqr+xsqr(1,i); i=i+1; end
% To calculate summation of x % To calculate summation of y % To calculate summation of x*y % To calculate summation of x^2 % To calculate log of y % To calculate log of x

disp(xa); disp(ya) beta=(n*XY-Y*X)/(n*Xsqr-X^2); a0=(Y*Xsqr-X*XY)/(n*Xsqr-X^2); alpha=10^(a0); ym=Y/n; sr(1,1)=0;j=1; while j<=n sr(1,j)=(y(1,j)-a0-beta*x(1,j))^2;
% To calculate sr for each x % To calculate co-eff of x^a0

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

st(1,j)=(y(1,j)-ym)^2; j=j+1; end SR=sum(sr); ST=sum(st); r2=(ST-SR)/ST

% To calculate st for each x

s=sprintf('Best fit curve (polynomial) for above data is given by : y = %f * x^(%f) ',alpha,beta); disp(s); xp = linspace(min(x),max(x)); yp = (xp.^beta)*alpha; plot(xa,ya,'o') hold on plot(xp,yp) grid on; xlabel('values of x'); ylabel('values of y'); title('y=alpha*x^(beta)');

Output :
Enter row matrix x : [26.67 93.33 148.89 315.56] Enter row matrix y : [1.35 0.085 0.012 0.00075] 26.6700 1.3500 r2 = 0.9757 Best fit curve (polynomial) for above data is given by : y = 38147.936083 * x^(-3.013376) 93.3300 0.0850 148.8900 0.0120 315.5600 0.0008

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

y=alpha*(x)b 2 1.8 1.6 1.4

values of y

1.2 1 0.8 0.6 0.4 0.2 0

50

100

150 200 values of x

250

300

350

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 41 Statement: Write down the Matlab Program to fit curve y = a1 * e (b1*x) by using least square techniques for given set of points.

Solution: Input :
clc; clear all; x=input('Enter row matrix x : '); ya=input('Enter row matrix y : '); [m,n]=size(x);
% Defining size of matrix x % Defining matrix xy & y

xy(1,1)=0; y(1,1)=0; while i<=n;

i=1; X=0; Y=0; XY=0; Xsqr=0; % Setting initial condition for loop y(1,i)=log(ya(1,i)); xy(1,i)=x(1,i)*y(1,i); xsqr(1,i)=x(1,i)^2; X=X+x(1,i); Y=Y+y(1,i); XY=XY+xy(1,i); Xsqr=Xsqr+xsqr(1,i); i=i+1; end disp(x); disp(ya); a1=(n*XY-Y*X)/(n*Xsqr-X^2); a0=(Y*Xsqr-X*XY)/(n*Xsqr-X^2); alpha=exp(a0); ym=Y/n; while j<=n; sr(1,j)=(y(1,j)-a0-a1*x(1,j))^2; st(1,j)=(y(1,j)-ym)^2; j=j+1;
% Finding mean

sr(1,1)=0;j=1;

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

end xp = linspace(min(x),max(x)); yp= alpha*exp(a1*xp);


% Condition for graph % Given function

SR=sum(sr); ST=sum(st); r2=(ST-SR)/ST


% Co-efficient of determination

s=sprintf('Best fit curve (exponential) for above data is given by : y = %f * e^(%f * x) ',alpha,a1); disp(s);

plot(x,ya,'o',xp,yp) grid on;

% Plots function & best fitted curve simultaneously

xlabel('values of x'); % Defining specifications of graph ylabel('values of y'); title('y=alpha*e^(beta*x)'); grid on;
% To display grid on graph

Output :
Enter row matrix x : [0.4 0.8 1.2 1.6 2.0 2.3] Enter row matrix y : [800 975 1500 1950 2900 3600] 0.4000 800 0.8000 975 1.2000 1500 1.6000 1950 2.0000 2.3000 2900 3600

r2 = 0.9933 Best fit curve (exponential) for above data is given by : y = 546.590939 * e^(0.818651 * x)

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

y=alpha*e(beta*x) 4000

3500

3000

values of y

2500

2000

1500

1000

500 0.4

0.6

0.8

1.2

1.4 1.6 values of x

1.8

2.2

2.4

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 11 Statement: Write down the Matlab Program for Lagrange Interpolation. Solution: Input :
clc; clear all; x = input('Enter the of Values of x: '); y = input('Enter the of Values of y: '); u = input('Value of x at which y is to be evaluated: '); n = length(x); % Size of matrix x p=1; s=0; for i=1:n p=y(i); for j=1:n if (i~=j) % Condition for inequality p=p*(u-x(j))/(x(i)-x(j)); % Formula end end s=s+p; % Summation end fprintf('\n Value of y at required x is : %f ',s);

Output :
Enter the of Values of x: [1 4 5 7] Enter the of Values of y: [21.746 438.171 1188.9147 8775.011] Value of x at which y is to be evaluated: 4.2 Value of y at required x is : 490.360287

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 12 Statement: Write down the Matlab Program for Newton-Gregory Forward Difference Interpolation. Solution: Input :
clc; clear all; x=input('Enter row matrix x : '); y=input('Enter row matrix y : '); X=input('Enter value of x at which value of function is to be calculated : '); [m,n]=size(x); dx=diff(x); d(1,1)=y(1,1); disp(x); disp(y); for j=1:(n-1) dy=diff(y); disp(dy); d(j+1)=dy(1); y=dy; end alpha=(X-x(1))/dx(1); a(1,1)=1; prod=1; for k=1:(n-2) prod=prod*(alpha-k+1); a(k+1)=prod; end func=0; for i=1:n-1 fx=a(i)*d(i)/(factorial(i-1)); func=func+fx; end
% Value of alpha is calculated. % Stores 1st value of delta matrix. % Delta matrix % Spatial diff.(for equally spaced data)

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

s=sprintf('Value of function calculated by N-G forward interpolation : %f',func); disp(s);

Output :
Enter row matrix x : [2 3 4 5 6 7 8 9] Enter row matrix y : [19 48 99 178 291 444 643 894] Enter value of x at which value of function is to be calculated : 3.5 2 19 29 22 6 0 0 0 0 Value of function calculated by N-G forward interpolation : 70.375000 3 48 51 28 6 0 0 0 4 99 79 34 6 0 0 5 178 113 40 6 0 6 291 153 46 6 7 444 199 52 8 643 251 9 894

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 13 Statement: Write down the Matlab Program for Newton-Gregory Backward Difference Interpolation. Solution: Input :
clc; clear all; x=input('Enter row matrix x : '); y=input('Enter row matrix y : '); X=input('Enter value of x at which value of function is to be calculated : '); [m,n]=size(x); dx=diff(x); d(1,1)=y(n);
% Spatial diff.(for equally spaced data)

newx(1,n:-1:1)=x(1,1:n);
brought 1st.

% Reversing order of matrix x so that nth value is

newy(1,n:-1:1)=y(1,1:n);
brought 1st.

% Reversing order of matrix y so that nth value is

disp(newx) disp(newy) for j=1:(n-1) dy=diff(newy); disp(dy); d(j+1)=dy(1); newy=dy; end alpha=(x(n)-X)/dx(1); a(1,1)=1; prod=1; for k=1:(n-2) prod=prod*(alpha-k+1); a(k+1)=prod; end
% Value of alpha is calculated. % Stores 1st value of delta matrix. % Delta matrix

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

func=0; for i=1:n-1 fx=a(i)*d(i)/(factorial(i-1)); func=func+fx; end s=sprintf('Value of function calculated by N-G backward interpolation : %f',func); disp(s);

Output :
Enter row matrix x : [0.1 0.2 0.3 0.4 0.5] Enter row matrix y : [1.4 1.56 1.76 2 2.28] Enter value of x at which value of function is to be calculated : 0.25 0.5000 2.2800 -0.2800 0.0400 0.4000 2.0000 -0.2400 0.0400 0.3000 1.7600 -0.2000 0.0400 -0.2220 0.2000 1.5600 -0.1600 0.1000 1.4000

1.0e-015*0.2220 -4.4409e-016

Value of function calculated by N-G backward interpolation : 1.655000

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 14 Statement: Write down the Matlab Program for Hermite interpolation method.

Solution: Input :
clc; clear all; disp('HERMITE INTERPOLATION'); x=input('Enter the values of x: '); xu=input('Enter the value unknown of x: '); fx=input('Enter the values of fx: '); dfx=input('Enter the values of dfx: '); n=size(x); % Size of matrix sum=0;suma=0;sumb=0; for i=1:n pro=1; pro1=1; for j=1:n if i~=j pro=pro*(xu-x(j))/(x(i)-x(j)); % Lagrange formulation of unknown x. pro1=pro1*(x(i)-x(j)); % Derivative of Lagrange term end end L(i,1)=pro; % Lagrange term dL(i,1)=pro1; % Derivative of Lagrange term end for k=1:n suma=suma+(1-2*(xu-x(k))*dL(k))*((L(k))^2)*fx(k); % Summation sumb=sumb+(xu-x(k))*((L(k))^2)*dfx(k); end sumf=suma+sumb; disp('The value of fx at unknown x is: '); disp(sumf);

Output:
HERMITE INTERPOLATION Enter the values of x: [0;1] Enter the value unknown of x: 0.4 Enter the values of fx: [0;1] Enter the values of dfx: [0;2] The value of fx at unknown x is: 0.1600

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 15 Statement: Write down the Matlab Program for interpolation by Cubic spline.

Solution: Input :

% Clearing Workspace

clear all; clc; close;


% Defining Input points

x1=input('Enter matrix for values of x: '); y1=input('Enter matrix for values of y: '); xg=input('Enter value of x for which to find y: '); m1=size(x1); n=m1(1,2); x=x1'; y=y1'; scatter(x,y); hold on;
% MATLAB function plotting Cubic Interpolation

yy = spline(x,y,0:0.01:100); plot(x,y,'o',0:0.01:100,yy);
% Defining end conditions f''(x)=0 @ 1st and last point

M(1:n+1)=0;
% First row of matrix to be solved

A(1,1:3)=[2*(x(3)-x(1)) (x(3)-x(2)) 0]; B(1,1)=6*(y(3)-y(2))/(x(3)-x(2))-6*(y(2)-y(1))/(x(2)-x(1));


% Subsequent rows till n-2

if n>3 for l=2:n-2 A(l,l-1:l+1)=[(x(l+1)-x(l)) 2*(x(l+2)-x(l)) (x(l+2)-x(l+1))]; B(l,1)=6*(y(l+2)-y(l+1))/(x(l+2)-x(l+1))-6*(y(l+1)-y(l))/(x(l+1)x(l)); end end


% Last 1 row

A(n-1,n-2:n-1)=[(x(n)-x(n-1)) 2*(x(n)-x(n-1))]; B(n-1,1)=-6*(y(n)-y(n-1))/(x(n)-x(n-1));

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

% Finding other values of f''(x)

N=GaussSoln(A,B);
% Assigning Values to M

for i=1:n-1 M(i+1)=N(i); end


% Creating the interpolation function between intervals

f=inline('Ma/6/(xb-xa)*(xb-xx)^3-Mb/6/(xb-xa)*(xa-xx)^3+(ya/(xb-xa)-Ma*(xbxa)/6)*(xb-xx)-(yb/(xb-xa)-Mb*(xb-xa)/6)*(xaxx)','xx','Ma','Mb','xa','xb','ya','yb');
% Ploting the spline in intervals

xn(1:1000)=0; yn(1:1000)=0; for i=1:n-1 j=1; dx=(x(i+1)-x(i))/1000; for k=x(i):dx:x(i+1) xn(j)=k; yn(j)=f(k,M(i),M(i+1),x(i),x(i+1),y(i),y(i+1)); j=j+1; end if xg>=x(i) && xg<=x(i+1) yg=f(xg,M(i),M(i+1),x(i),x(i+1),y(i),y(i+1)); end plot(xn,yn, 'LineWidth',2); xlim([min(x) max(x)]); ylim([min(y) max(y)]); end hold off; fprintf('@x=%f, y=%f\n',xg,yg);

GaussSoln:

function Soln=GaussSoln(x,y) A1=x; B=y; n2=size(B); n=n2(1,1); clear x; clear y; if det(A1)==0 disp('Either no solution or infinitely many solutions.'); else A=A1; A(:,n+1)=B(1:n);

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

for i=1:n-1 for j=i:n-1 fac=A(j+1,i)/A(i,i); fac_mat=fac*A(i,:); A(j+1,:)=A(j+1,:)-fac_mat; end end i=0;j=0; if A(n,n)==0 an(n)=0; else an(n)=A(n,n+1)/A(n,n); end for i=n-1:-1:1 for j=n:-1:1 x(j)=an(j)*A(i,j); end y=sum(x); if y==0 an(i)=0; else an(i)=(A(i,n+1)-y)/A(i,i); end end end Soln=an;

Output:
Enter matrix for values of x: [1 2 3 4] Enter matrix for values of y: [0 0.3 0.48 0.6] Enter value of x for which to find y: 2.3 @x=2.300000, y=0.363014

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 16 Statement: Write down the Matlab Program for Inverse Interpolation. Solution: Input :
clc; clear all; x = input('Enter the of Values of x: '); y = input('Enter the of Values of y: '); r = input('Value of y at which x is to be evaluated: '); n = length(x); % determines size of matrix. p=1; s=0; for j=1:n for i=1:n if i==j continue; end numerator=r-y(i); denominator=y(j)-y(i); v(j)=numerator/denominator; p=p*v(j); end s=s+p*x(j); p=1; end fprintf('\n Value is : %f ',s)

Output :
Enter the of Values of x: [0 1 2 3] Enter the of Values of y: [0 1 7 25] Value of y at which x is to be evaluated: 2 Value is : 1.716138

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 17 Statement: Write down the Matlab Program for Newton Forward Differentiation. Solution: Input :
clc; clear all; x=input('Enter row matrix x : '); y=input('Enter row matrix y : '); r=input('Enter value of x at which value of function is to be calculated : '); [m,n]=size(x); p=1; h=diff(x); % Step size disp(x); disp(y); for j=1:n if (r==x(j)) p=j; end end d(1,1)=y(1,p); for j=1:(n-p) dy=diff(y); % Delta matrix disp(dy); y=dy; d(j+1)=y(1,p); % Stores p th value of delta matrix. end f=0; for k=1:n-1 fr=d(k+1)/k; f=f+((-1)^(k-1))*fr; end dx=(f/h(1)); s=sprintf('Value of dy/dx at %f is : % f',r,dx); disp (s);

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Output :
Enter row matrix x : [1.5 2 2.5 3 3.5 4] Enter row matrix y : [3.375 7 13.625 24 38.875 59] Enter value of x at which value of function is to be calculated : 1.5 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000

3.3750

7.0000

13.6250

24.0000

38.8750

59.0000

3.6250

6.6250

10.3750

14.8750

20.1250

3.0000

3.7500

4.5000

5.2500

0.7500

0.7500

0.7500

Value of dy/dx at 1.500000 is :

4.750000

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 18 Statement: Write down the Matlab Program for Newton Backward Differentiation. Solution: Input :
clc; clear all; xin=input('Enter row matrix x : '); yin=input('Enter row matrix y : '); r=input('Enter value of x at which value of function is to be calculated : '); [m,n]=size(xin); p=1; h=diff(xin); % Step size y(1,n:-1:1)=yin(1,1:n); % Reversing order of matrix y so that nth value is brought
1st.

x(1,n:-1:1)=xin(1,1:n);
1st.

% Reversing order of matrix x so that nth value is brought

disp(x) disp(y) for j=1:n if (r==x(j)) p=j; end end d(1,1)=y(1,p); for j=1:(n-p) dy=diff(y); % Delta matrix y=(-1)*dy; d(j+1)=(y(1,p)); % Stores p th value of delta matrix. disp(y); end f=0; for k=1:n-1 fr=d(k+1)/k; f=f+fr; end dx=(f/h(1)); s=sprintf('Value of dy/dx at %f is : % f',r,dx); disp (s);

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Output :
Enter row matrix x : [0 10 20 30 40] Enter row matrix y : [1 0.984 0.939 0.866 0.766] Enter value of x at which value of function is to be calculated : 40 40 30 20 10 0

0.7660

0.8660

0.9390

0.9840

1.0000

-0.1000

-0.0730

-0.0450

-0.0160

-0.0270

-0.0280

-0.0290

0.0010

0.0010

-2.2204e-016

Value of dy/dx at 40.000000 is : -0.011317

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 19 Statement: Write down the Matlab Program using Trapezoidal rule(single segment) for any function.

Solution: Input :
clear; clc; fprintf('NUMERICAL INTEGRATION BY TRAPEZOIDAL RULE \n\n'); x=input('Enter a function to integrate f(x)=','s'); a=input('Enter Lower Limit: '); b=input('Enter Upper Limit: '); n=2; % No. of points y=inline(x); % Defining function h=(b-a)/n; % Step size S=0; for i=1:n-1; t=2*y(a+i*h); S=S+t; end A=h/2*(y(a)+y(b)+S); % Calculation of area fprintf('\nAnswer= %f\n',A);

Output :
NUMERICAL INTEGRATION BY TRAPEZOIDAL RULE

Enter a function to integrate f(x)=4*x+2 Enter Lower Limit: 1 Enter Upper Limit: 4

Answer= 36.000000

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 20 Statement: Write down the Matlab Program using Trapezoidal rule(multiple segment) for any function.

Solution: Input :
clear; clc; fprintf('NUMERICAL INTEGRATION BY TRAPEZOIDAL RULE \n\n'); x=input('Enter a function to integrate f(x)=','s'); a=input('Enter Lower Limit: '); b=input('Enter Upper Limit: '); n=input('enter no. of segments'); y=inline(x); % Defining function h=(b-a)/n; % Step size S=0; for i=1:n-1; t=2*y(a+i*h); S=S+t; end A=h/2*(y(a)+y(b)+S); % Calculation of area fprintf('\nAnswer= %f\n',A);

Output :
NUMERICAL INTEGRATION BY TRAPEZOIDAL RULE

Enter a function to integrate f(x)=4*x+2 Enter Lower Limit: 1 Enter Upper Limit: 4 enter no. of segments6

Answer= 36.000000

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 21 Statement: Write down the Matlab Program using Simpsons 1/3rd (single segment) rule for any function.

Solution: Input :
clear; clc; fprintf('NUMERICAL INTEGRATION BY SIMPSONS 1/3 RULE \n\n'); x=input('Enter a function to integrate f(x)=','s'); a=input('Enter Lower Limit: '); b=input('Enter Upper Limit: '); n=2; % No. of segment y=inline(x); h=(b-a)/n; S=0; for i=1:n-1; if mod(i,2)==1 % Condition for even segments t=4*y(a+i*h); else t=2*y(a+i*h); end S=S+t; end A=h/3*(y(a)+y(b)+S); fprintf('\nAnswer= %f\n',A);

Output :
NUMERICAL INTEGRATION BY SIMPSONS 1/3 RULE

Enter a function to integrate f(x)=exp(x) Enter Lower Limit: 0 Enter Upper Limit: 4

Answer= 44.247402

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 22 Statement: Write down the Matlab Program using Simpsons 1/3rd (multiple segment) rule for any function.

Solution: Input :
clear; clc; fprintf('NUMERICAL INTEGRATION BY SIMPSONS 1/3 RULE \n\n'); x=input('Enter a function to integrate f(x)=','s'); a=input('Enter Lower Limit: '); b=input('Enter Upper Limit: '); n=input(Enter no. of divisions: ); y=inline(x); h=(b-a)/n; S=0; for i=1:n-1; if mod(i,2)==1 t=4*y(a+i*h); else t=2*y(a+i*h); end S=S+t; end A=h/3*(y(a)+y(b)+S); fprintf('\nAnswer= %f\n',A);

Output :
NUMERICAL INTEGRATION BY SIMPSONS 1/3 RULE

Enter a function to integrate f(x)=exp(x) Enter Lower Limit: 0 Enter Upper Limit: 4 enter no.of divisions:5

Answer= 44.683772

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 23 Statement: Write down the Matlab Program using Simpsons 3/8th rule for any function.

Solution: Input :
clear; clc; fprintf('NUMERICAL INTEGRATION BY SIMPSONS 3/8 RULE \n\n'); x=input('Enter a function to integrate f(x)=','s'); a=input('Enter Lower Limit: '); b=input('Enter Upper Limit: '); while mod(n,3)~=0 % Condition for no. of segments n=input('Enter No. of Divisions [Should be divisible by 3]: '); end y=inline(x); % Defining function h=(b-a)/n; % Step size S=0; for i=1:n-1; if mod(i,3)==0 % Decision statement for usage of formula t=2*y(a+i*h); else t=3*y(a+i*h); end S=S+t; end A=3*h/8*(y(a)+y(b)+S); % Area calculation fprintf('\nAnswer= %f\n',A);

Output :
Enter the function: 4*x-1 Initial Value of x :1 Final Value of x :4 Enter No. of Divisions [Should be divisible by 3]: 3 Answer: 27.000000>>

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 24 Statement: Write down the Matlab Program for Combined Simpsons Rule. Solution: Input :
clear; clc; j=1; fprintf('NUMERICAL INTEGRATION BY MULTIPLE SIMPSONS RULE \n\n'); x=input('Enter a function to integrate f(x)=','s'); a=input('Enter Lower Limit: '); b=input('Enter Upper Limit: '); while j==1 n=input('Enter No. of Divisions [(n-3) divisible by 2]: '); for no. of segments if mod(n-3,2)==0 j=0; end end y=inline(x); h=(b-a)/n; S=0; if n>=3 for i=1:2; t=3*y(a+i*h); S=S+t; end A=3*h/8*(y(a)+y(a+3*h)+S); end S=0; for i=4:n-1; if mod(i,2)==0 t=4*y(a+i*h); else t=2*y(a+i*h); end S=S+t; end A=A+h/3*(y(a+3*h)+y(b)+S); fprintf('\nAnswer= %f\n',A);

% Condition

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

OUTPUT:
NUMERICAL INTEGRATION BY MULTIPLE SIMPSONS RULE Enter Enter Enter Enter a function to integrate f(x)=x^0.1*(1.2-x)*(1-exp(20*(x-1))) Lower Limit: 0 Upper Limit: 2 No. of Divisions [(n-3) divisible by 2]: 5

Answer= 55501691.391968 >>

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 25 Statement: Write down the Matlab Program for Gauss-Legendre 2-pt method. Solution: Input :

clear; clc; fprintf('NUMERICAL INTEGRATION BY GAUSS-LEGENDRE 2-POINT FORMULA \n\n'); x=input('Enter a function to integrate f(x)=','s'); a=input('Enter Lower Limit: '); b=input('Enter Upper Limit: '); f=inline(x); % Defining function c=(b-a)/2; % Constants d=(b+a)/2; % Constants x1=c/sqrt(3)+d; x2=-c/sqrt(3)+d; y1=f(x1); y2=f(x2); A=(y1+y2)*c; fprintf('\nAnswer= %f\n',A);

OUTPUT:
NUMERICAL INTEGRATION BY GAUSS-LEGENDRE 2-POINT FORMULA Enter a function to integrate f(x)=x^3+x-1 Enter Lower Limit: 1 Enter Upper Limit: 4 Answer= 68.250000

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 26 Statement: Write down the Matlab Program using Gauss Legendre 3-pt rule for any function.

Solution: Input :
clear; clc; fprintf('NUMERICAL INTEGRATION BY GAUSS-LEGENDRE 3-POINT FORMULA \n\n'); x=input('Enter a function to integrate f(x)=','s'); a=input('Enter Lower Limit: '); b=input('Enter Upper Limit: '); f=inline(x); % Defining function c=(b-a)/2; d=(b+a)/2; x1=c*sqrt(3/5)+d; x2=-c*sqrt(3/5)+d; x3=d; y1=f(x1); y2=f(x2); y3=f(x3); A=(5/9*y1+5/9*y2+8/9*y3)*c; fprintf('\n Answer= %f\n',A);

Output :
NUMERICAL INTEGRATION BY GAUSS-LEGENDRE 3-POINT FORMULA

Enter a function to integrate f(x)=x^2-5*x+2 Enter Lower Limit: 3 Enter Upper Limit: 5

Answer= -3.333333

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 27 Statement: Write down the Matlab Program using Double integration by trapezoidal rule for any function.

Solution: Input :
clear; clc;
% Taking Input

fprintf('DOUBLE INTEGRATION BY TRAPEZOIDAL RULE \n\n'); xy=input('Enter a function to integrate f(x,y)=','s'); ax=input('Enter Lower Limit of x: '); bx=input('Enter Upper Limit of x: '); ay=input('Enter Lower Limit of y: '); by=input('Enter Upper Limit of y: '); nx=input('No. of intervals for integration w.r.t. x: '); ny=input('No. of intervals for integration w.r.t. y: ');
% Defining the function

f=inline(xy);
% Main Calculations

h=(bx-ax)/nx; k=(by-ay)/ny; an=0; for i=0:nx-1 for j=0:ny-1 tr=f(ax+i*h,ay+j*k)+f(ax+i*h,ay+(j+1)*k)+f(ax+(i+1)*h,ay+(j+1)*k)+f(ax+(i+1)* h,ay+j*k); an=an+tr; end end A=h*k/4*an; fprintf('\nAnswer= %f\n',A);

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Output :
DOUBLE INTEGRATION BY TRAPEZOIDAL RULE

Enter a function to integrate f(x,y)=x+y Enter Lower Limit of x: 0 Enter Upper Limit of x: 2 Enter Lower Limit of y: 1 Enter Upper Limit of y: 3 No. of intervals for integration w.r.t. x: 2 No. of intervals for integration w.r.t. y: 2

Answer= 12.000000

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 28 Statement: Write down the Matlab Program using double integration by Simpsons 1/3rd rule for any function.

Solution: Input :
clear; clc;
% Taking Input

fprintf('DOUBLE INTEGRATION BY SIMPSONS 1/3rd RULE \n\n'); xy=input('Enter a function to integrate f(x,y)=','s'); ax=input('Enter Lower Limit of x: '); bx=input('Enter Upper Limit of x: '); ay=input('Enter Lower Limit of y: '); by=input('Enter Upper Limit of y: '); nx=3; ny=3; while mod(nx,2)~=0 || mod(ny,2)~=0 nx=input('No. of intervals for integration w.r.t. x (Should be even): '); ny=input('No. of intervals for integration w.r.t. y (Should be even): '); end
% Defining the function

f=inline(xy);
% Main Calculations

h=(bx-ax)/nx; k=(by-ay)/ny; an=0; for i=0:2:nx-1 for j=0:2:ny-1 tr1=f(ax+i*h,ay+j*k)+f(ax+i*h,ay+(j+2)*k)+f(ax+(i+2)*h,ay+(j+2)*k)+f(ax+(i+2) *h,ay+j*k); tr2=f(ax+i*h,ay+(j+1)*k)+f(ax+(i+1)*h,ay+(j+2)*k)+f(ax+(i+2)*h,ay+(j+1)*k)+f( ax+(i+1)*h,ay+j*k); tr3=f(ax+(i+1)*h,ay+(j+1)*k); an=an+tr1+4*tr2+16*tr3; end end A=h*k/9*an; fprintf('\nAnswer= %f\n',A);

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Output :
DOUBLE INTEGRATION BY SIMPSONS 1/3rd RULE

Enter a function to integrate f(x,y)=x-y+1 Enter Lower Limit of x: 6 Enter Upper Limit of x: 14 Enter Lower Limit of y: 1 Enter Upper Limit of y: 5 No. of intervals for integration w.r.t. x (Should be even): 4 No. of intervals for integration w.r.t. y (Should be even): 4

Answer= 256.000000

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 29 Statement: Write down the Matlab Program for Euler Method.

Solution: Input :
clc; clear all; dydx=input('Emter A Function dy/dx : ','s'); x0=input('Enter The Initial Value of x :'); y0=input('Enter The Initial Value of y :'); xf=input('Enter Value of "x" At Which Value of "y" Is To Be Found: '); h=input('Enter Step Size :'); f=inline(dydx); % Defining function n=(xf-x0)/h; for i=1:n y(i) = y0 + h*(f(x0,y0)); % Evaluating function at given x & y y0 = y(i); x0 = x0 + h; end s=sprintf('\n Value of y At x = %f Is : %f',xf,y(n)); disp(s);

Output :
Enter Enter Enter Enter Enter A Function dy/dx : (x+y)/((y^2)-(sqrt(x*y))) The Initial Value of x :1.3 The Initial Value of y :2 Value of "x" At Which Value of "y" Is To Be Found: 1.8 Step Size :.05

Value of y At x = 1.800000 Is : 2.578164

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 30 Statement: Write down the Matlab Program for Heuns method.

Solution: Input :

clc; clear all; disp('HEUNS METHOD'); format long; dydx=input('\nEnter The Function dy/dx : ','s'); x0=input('Enter The Initial Value of x: '); y0=input('Enter Initial Value of y: '); h=input('Enter step size: '); xf=input('Enter Value of x For Which y Is To Be Found: '); fprintf('\n'); f=inline(dydx); n=(xf-x0)/h; for i=1:n yf = y0 + h*f(x0,y0); yff = y0 + h*(f(x0,y0) + f(x0+h,yf))/2; y0 = yff; x0 = x0 + h; s = sprintf('Value y = %f At x%d',yff,i); disp(s); end

Output :
HEUNS METHOD Enter Enter Enter Enter Enter Value Value Value Value The Function dy/dx : 4*exp(.8*x) - .5*y The Initial Value of x: 0 Initial Value of y: 2 step size: 1 Value of x For Which y Is To Be Found: 4 y y y y = = = = 6.701082 At x1 16.319782 At x2 37.199249 At x3 83.337767 At x4

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 31 Statement: Write down the Matlab Program for Modified Euler method. Solution: Input:
clc; clear all; % Clears the workspace disp('MODIFIED EULER METHOD'); format long % Take the input from user eq=input('\nEnter the diff. eqn in x and y: ','s'); s=inline(eq); y0=input('Enter y: '); x0=input('Enter x: '); xu=input('Enter unknown x: '); acc=input('Enter accuracy required: '); % Calculatoins h=(xu-x0)/2;n=2; for i=1:n x1=x0+h; y1=y0+h*s(x0,y0); y1n=y0+(h/2)*(s(x0,y0)+s(x1,y1)); dy=abs(y1-y1n); while dy>acc y1=y1n; y1n=y0+(h/2)*(s(x0,y0)+s(x1,y1)); dy=abs(y1-y1n); end x0=x1; y0=y1n; end % Prints the answer disp('The value of the diff eqn at unkown x is: '); disp(y1n);

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Output:
MODIFIED EULER METHOD Enter Enter Enter Enter Enter the diff. eqn in x and y: sqrt(x+y) y: 2.2 x: 1 unknown x: 1.2 accuracy required: 0.0001 The value of the diff eqn at unkown x is: 2.573186212370175

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 32 Statement: Write down the Matlab Program for Runge-Kutta 2nd order method.

Solution: Input:
clc; clear all; % Clears the workspace disp('RUNGE KUTTA METHOD 2ND ORDER'); format long % Takes the input from user eq=input('Enter the diff. eqn in x and y: ','s'); s=inline(eq); % Converts the i/p string into symbolic function y0=input('Enter y: '); x0=input('Enter x: '); xu=input('Enter unknown x: '); h=input('Enter step size: '); n=(xu-x0)/h; for i=1:n+1 x1=x0+h; y1=y0+h*s(x0,y0); c1=h*s(x0,y0); c2=h*s(x1,y1); c=(c1+c2)/2; yans=y0+c; y0=yans; x0=x1; end % Prints the answer disp('The value of the diff eqn at unkown x is: '); disp(yans);

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Output:
RUNGE KUTTA METHOD 2ND ORDER Enter the diff. eqn in x and y: -(y+x*y^2) Enter y: 1 Enter x: 0 Enter unknown x: 0.3 Enter step size: 0.1 The value of the diff eqn at unkown x is: 0.715327926979073

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 33 Statement: Write down the Matlab Program for Runge-Kutta 4th order method. Solution: Input:
clc; clear all; % Clears the workspace disp('RUNGE KUTTA METHOD 4TH ORDER'); format long % Takes the input from user eq=input('Enter the diff. eqn in x and y: ','s'); s=inline(eq); % Converts the i/p string into symbolic function y0=input('Enter y: '); x0=input('Enter x: '); xu=input('Enter unknown x: '); h=input('Enter step size: '); % Calculation n=(xu-x0)/h; for i=1:n x1=x0+h; y1=y0+h*s(x0,y0); c1=h*s(x0,y0); c2=h*s((x0+(h/2)),(y0+(c1/2))); c3=h*s((x0+(h/2)),(y0+(c2/2))); c4=h*s(x1,(y0+c3)); c=(c1+2*c2+2*c3+c4)/6; yans=y0+c; y0=yans; x0=x1; end % Prints the answer disp('The value of the diff eqn at unkown x is: '); disp(yans);

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Output:
RUNGE KUTTA METHOD 4TH ORDER Enter the diff. eqn in x and y: 0*x+y Enter y: 2 Enter x: 0 Enter unknown x: 0.2 Enter step size: 0.1 The value of the diff eqn at unkown x is: 2.442805141701389

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 34 Statement: Write down the Matlab Program for Milnes correct prediction method . Solution: Input:
clc; clear all; % Clears the workspace disp('MILNE PREDICTION'); format long % Take the input from user eq=input('Enter the 1st diff. eqn in x, y: ','s'); s=inline(eq); y=input('Enter y: '); x=input('Enter x: '); xu=input('Enter unknown x: '); h=input('Enter step size: '); %calculation n=(xu-x(4))/h; f1=s(x(2),y(2)); f2=s(x(3),y(3)); f3=s(x(4),y(4)); for i=1:n+1 y4pr=y(1)+(4*h/3)*(2*f1-f2+2*f3); f4pr=s(xu-h*(n-i),y4pr); y4cr=y(3)+(h/3)*(f2+4*f3+f4pr); if y4pr~=y4cr y4pr=y4cr; y4=y4cr; end f4=s(xu-h*(n-i),y4); f1=f2;f2=f3;f3=f4; y(1)=y(2); y(3)=y(4); yans=y4cr; end disp('The value of the diff eqn at unkown x is: '); disp(yans);

Output:
MILNE Enter Enter Enter Enter Enter PREDICTION the 1st diff. eqn in x, y: x-y+1 y: [0;0.1951;0.3812;0.5591] x: [1;1.1;1.2;1.3] unknown x: 1.5 step size: 0.1

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

The value of the diff eqn at unkown x is: 0.893399346172840

Assignment No: 35 Statement: Write down the Matlab Program for Runge-Kutta simultaneous method. Solution: Input:
clc; clear all; % Clears the workspace disp('RUNGE KUTTA METHOD 4TH ORDER FOR SIMLTANEOUS EQUATONS'); format long % Takes the input from user eq=input('Enter the 1st diff. eqn in x, y, z: ','s'); eq1=input('Enter the 2nd diff. eqn in x, y, z: ','s'); s=inline(eq,'x','y','z'); % Converts the i/p string into symbolic function s1=inline(eq1,'x','y','z'); % Converts the i/p string into symbolic function y0=input('Enter y: '); x0=input('Enter x: '); z0=input('Enter z: '); xu=input('Enter unknown x: '); h=input('Enter step size: '); % Calculation n=(xu-x0)/h; for i=1:n x1=x0+h; c1=h*s(x0,y0,z0); d1=h*s1(x0,y0,z0); c2=h*s((x0+(h/2)),(y0+(c1/2)),(z0+(d1/2))); d2=h*s1((x0+(h/2)),(y0+(c1/2)),(z0+(d1/2))); c3=h*s((x0+(h/2)),(y0+(c2/2)),(z0+(d2/2))); d3=h*s1((x0+(h/2)),(y0+(c2/2)),(z0+(d2/2))); c4=h*s(x1,(y0+c3),(z0+d3)); d4=h*s1(x1,(y0+c3),(z0+d3)); c=(c1+2*c2+2*c3+c4)/6; d=(d1+2*d2+2*d3+d4)/6; yans=y0+c; zans=z0+d; y0=yans; z0=zans; x0=x1; end % Prints the answer disp('The value of the diff eqn at unknown x is: '); disp(yans);

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

disp('The value of the differential at unknown x is: '); disp(zans);

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Output:
RUNGE KUTTA METHOD 4TH ORDER FOR SIMLTANEOUS EQUATONS Enter the 1st diff. eqn in x, y, z: x+y*z Enter the 2nd diff. eqn in x, y, z: x^2-y^2 Enter y: 1 Enter x: 0 Enter z: 0.5 Enter unknown x: 1.2 Enter step size: 1.2 The value of the diff eqn at unknown x is: 1.352724056760832 The value of the differential at unknown x is: -0.775714711925248

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: Statement: Write down the Matlab Program for Adams Bashforth.

Solution: Input:
clear; clc; % Clears the work space % Get the input from user g=input('Enter the function dy/dx: ','s'); x=input('Enter values of x: '); y=input('Enter values of y: '); xg=input('Enter x at which value is to be found: '); h=input('Enter step size: '); f=inline(g); % Convert the input string into a symbolic function m=size(x); % Calculate the size of matrix x % Main calculation n=(xg-x(4))/h; for i=1:n ya=y(4)+(h/24)*(-9*(f(x(1),y(1)))+(37*(f(x(2),y(2))))(59*(f(x(3),y(3))))+(55*(f(x(4),y(4))))); ya1=y(4)+(h/24)*((f(x(2),y(2)))(5*(f(x(3),y(3))))+(19*(f(x(4),y(4))))+(9*f(x(4)+h,ya))); while(ya1~=ya) ya=ya1; ya1=y(4)+(h/24)*((f(x(2),y(2)))(5*(f(x(3),y(3))))+(19*(f(x(4),y(4))))+(9*f(x(4)+h,ya))); end for j=1:m-1 x(j)=x(j+1); y(j)=y(j+1); end x(m)=x(m)+h; y(4)=ya1; end fprintf('The value at given x is : %f \n',ya1); % Prints the answer

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

OUTPUT:
Enter the function dy/dx: 1+x*y^2 Enter values of x: [0 0.1 0.2 0.3] Enter values of y: [0.2 0.3003 0.4022 0.5075] Enter x at which value is to be found: 0.5 Enter step size: 0.1 The value at given x is : 0.740490

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 36 Statement: Write down the Matlab Program for Parabolic method.

Solution: Input :
% Program for Parabollic Equation (Schmidt Method) clear all; clc; a=1; b=1; % input xi=input('Enter initial value of x: '); xf=input('Enter final value of x: '); while a==1 h=input('Enter step size for x: '); co=(xf-xi)*10000/(h*10000); if mod(co,1)==0 a=0; end end ti=input('Enter initial value of t: '); tf=input('Enter final value of t: '); while b==1 k=input('Enter step size for t: '); ro=(tf-ti)*10000/(k*10000); if mod(ro,1)==0 b=0; end end s=input('For all values of x at t=0, u(x)=','s'); f=inline(s); C=input('Enter value of C: '); r=k/h^2*C^2; % Assign side values in matrix u(1,2:co+2)=xi:h:xf; u(2:ro+2,1)=ti:k:tf; u(2:ro+2,2)=input('Enter constant value of u for x=xi: '); u(2:ro+2,co+2)=input('Enter constant value of u for x=xf: '); % Assign central values in matrix by finding them for i=3:co+1 u(2,i)=f(u(1,i)); end for i=3:ro+2 for j=3:co+1 u(i,j)=r*u(i-1,j-1)+(1-2*r)*u(i-1,j)+r*u(i-1,j+1); end end % display output

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

disp(u);

Output:
Enter initial value of x: 0 Enter final value of x: 1 Enter step size for x: 0.2 Enter initial value of t: 0 Enter final value of t: 0.006 Enter step size for t: 0.002 For all values of x at t=0, u(x)=sin(pi*x) Enter value of C: 1 Enter constant value of u for x=xi: 0 Enter constant value of u for x=xf: 0 0 0 0.0020 0.0040 0.0060 0 0 0 0 0 0.2000 0.5878 0.5766 0.5655 0.5547 0.4000 0.9511 0.9329 0.9151 0.8976 0.6000 0.9511 0.9329 0.9151 0.8976 0.8000 0.5878 0.5766 0.5655 0.5547 1.0000 0 0 0 0

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 37 Statement: Write down the Matlab Program for Crank-Nicholeson method.

Solution: Input :
% Crank Nicoleson clear all; clc; a=1; b=1; c=1; % input xi=input('Enter initial value of x: '); xf=input('Enter final value of x: '); h=input('Enter step size for x: '); co=(xf-xi)*10000/(h*10000); ti=input('Enter initial value of t: '); tf=input('Enter final value of t: '); k=input('Enter step size for t: '); ro=(tf-ti)*10000/(k*10000); s=input('For all values of x at t=0, u(x)=','s'); f=inline(s); C=input('Enter value of C: '); r=k*C^2/h^2; % define side values of matrix u(1,2:co+2)=xi:h:xf; u(2:ro+2,1)=ti:k:tf; u(2:ro+2,2)=input('Enter constant value of u for x=xi: '); u(2:ro+2,co+2)=input('Enter constant value of u for x=xf: '); for i=3:co+1 u(2,i)=f(u(1,i)); end ui=u; k=1; % find central values of matrix while c==1 && k<=1000 ui=u; for i=2:ro+1 for j=3:co+1 %u(i+1,j)=r/(2*(1+r))*(u(i+1,j-1)+u(i+1,j+1)+u(i,j-1)-2*u(i,j)u(i,j+1))+u(i,j)/(1+r); u(i+1,j)=1/4*(u(i+1,j-1)+u(i+1,j+1)+u(i,j-1)+u(i,j+1)); end end k=k+1; uf=(u-ui)./u; if max(max(uf))<=0.001 c=0; end end

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

disp(u);

Output:

Enter initial value of x: 0 Enter final value of x: 3 Enter step size for x: 1 Enter initial value of t: 0 Enter final value of t: .3 Enter step size for t: .1 For all values of x at t=0, u(x)=x^2 Enter value of C: 1 Enter constant value of u for x=xi: 0 Enter constant value of u for x=xf: 0 0 0 0.1000 0.2000 0.3000 0 0 0 0 0 1.0000 1.0000 1.1333 0.2178 0.1046 2.0000 4.0000 0.5333 0.3378 0.0806 3.0000 0 0 0 0

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 38 Statement: Write down the Matlab Program for Hyperbolic method.

Solution: Input :
% Program to solve Hyperbolic Partial Differential Equation clear all; clc; a=1; b=1; % input xi=input('Enter initial value of x: '); xf=input('Enter final value of x: '); h=input('Enter step size for x: '); co=(xf-xi)*10000/(h*10000); ti=input('Enter initial value of t: '); tf=input('Enter final value of t: '); k=input('Enter step size for t: '); ro=(tf-ti)*10000/(k*10000); s=input('For all values of x at t=0, u(x)=','s'); f=inline(s); C=input('Enter value of C: '); r=h/k; if r~=C error('r is not equal to C'); end % Assign side values in matrix u(1,2:co+2)=xi:h:xf; u(2:ro+2,1)=ti:k:tf; u(2:ro+2,2)=input('Enter constant value of u for x=xi: '); u(2:ro+2,co+2)=input('Enter constant value of u for x=xf: '); % Assign unknown values in matrix for i=3:co+1 u(2,i)=f(u(1,i)); end for i=3:co+1 u(3,i)=(u(2,i-1)+u(2,i+1))/2; end for i=4:ro+2 for j=3:co+1 u(i,j)=u(i-1,j-1)+u(i-1,j+1)-u(i-2,j); end end % display output disp(u);

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Output:

Enter initial value of x: 0 Enter final value of x: 4 Enter step size for x: 1 Enter initial value of t: 0 Enter final value of t: 2.5 Enter step size for t: 0.5 For all values of x at t=0, u(x)=(x^2)*(2-x) Enter value of C: 2 Enter constant value of u for x=xi: 0 Enter constant value of u for x=xf: 0 0 0 0.5000 1.0000 1.5000 2.0000 2.5000 0 0 0 0 0 0 0 1.0000 1.0000 0 -5.0000 0 9.0000 0 2.0000 0 -4.0000 0 4.0000 0 4.0000 3.0000 -9.0000 0 5.0000 0 -1.0000 0 4.0000 0 0 0 0 0 0

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Assignment No: 39 Statement: Write down the Matlab Program for Elliptical method.

Solution: Input :
clear all; clc; % take user input u=input('Temperature of upper surface: '); l=input('Temperature of left surface: '); r=input('Temperature of right surface: '); b=input('Temperature of lower surface: '); cs=input('No. of elements in a row: '); n=cs-1; % Create a equation matrix an(n,n)=0; for i=1:n^2 for j=1:n^2 if i==j an(i,j)=4; elseif mod(i,n)==1 && j==i+1 an(i,j)=-1; elseif j==i-n && j>0 an(i,j)=-1; elseif j==i+n && j<=n^2 an(i,j)=-1; elseif mod(i,n)==0 && j==i-1 an(i,j)=-1; elseif mod(i,n)>1 && ( j==i+1 || j==i-1 ) an(i,j)=-1; end end end so(n)=0; for i=1:n^2 if i==1 so(i)=u+l; elseif i>1 && i<n so(i)=u; elseif i==n so(i)=u+r; elseif mod(i,n)==1 && i>n && i<=n^2-n so(i)=l; elseif mod(i,n)>1 && mod(i,n)<n && i>n && i<=n^2-n so(i)=0; elseif mod(i,n)==0 && i>n && i<=n^2-n so(i)=r; elseif i==n^2-n+1

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

so(i)=l+b; elseif i>n^2-n+1 && i<n^2 so(i)=b; elseif i==n^2 so(i)=b+r; end end an so an1=an; clear an; % solve the matrix t=GaussSoln(an1,so,n^2); k=1; % interpret the answers for i=1:n for j=1:n t1(i,j)=t(k); k=k+1; end end t1 hold off; % plot the answers for i=1:n for j=1:n scatter(i,j,80,[0.5 0 0],'filled'); s=sprintf('\n %1.2f',(t1(i,j))); text(j,i,s); hold on; end end axis ij; axis ([ 0 n+1 0 n+1]); hold off; GaussSoln: function Soln=GaussSoln(x,y,n1) A1=x; B=y; n=n1; clear x; clear y; % Check the conditions if det(A1)==0 disp('Either no solution or infinitely many solutions.'); else % forward elimination A=A1; A(:,n+1)=B(1:n); for i=1:n-1 for j=i:n-1 fac=A(j+1,i)/A(i,i);

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

fac_mat=fac*A(i,:); A(j+1,:)=A(j+1,:)-fac_mat; end end i=0;j=0; % Back substitution if A(n,n)==0 an(n)=0; else an(n)=A(n,n+1)/A(n,n); end for i=n-1:-1:1 for j=n:-1:1 x(j)=an(j)*A(i,j); end y=sum(x); if y==0 an(i)=0; else an(i)=(A(i,n+1)-y)/A(i,i); end end end % answer Soln=an;

Output:
Temperature of upper surface: 100 Temperature of left surface: 100 Temperature of right surface: 0 Temperature of lower surface: 0 No. of elements in a row: 3

an =

4 -1 -1 0

-1 4 0 -1

-1 0 4 -1

0 -1 -1 4

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

so =

200

100

100

t1 =

75.0000 50.0000

50.0000 25.0000

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Newton Raphson method

START

Set v=1500 vr=2500 g=9.81 m=2,00,000 uf=300

Read function (a) Derivative of function(b)

ft=inline(a) dft=inline(b)

Read significant digits n

epsilon_s= (0.5*10^(2-n)) epsilon_a=100

Input initial guess

td=tx

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

A A

while epsilon_a >=epsilon_s

tnew= td-(ft(td)/dft(td)) epsilon_a= abs((tnew-td)/tnew)*100 td=tnew

print error,tnew

print tnew

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Modified Newton Raphson method

START

Read function (a) Derivative of function(b) Second derivative (c)

t0=0 f=inline(a) df=inline(b) ddf=inline(c)

Read significant digits n

epsilon_s= (0.5*10^(2-n)) epsilon_a=100 tr=fzero(inline ft)

disp tr

Input initial guess

print head

disp head

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

A A

while (1)

tnew=told-((fx(told)*dfx(told)/((dfx(told)^2)-(fx(told)*d2fx(told))))) err=abs((tnew-told)/tnew)*100 epsilon_t=abs((tr-tnew)/tr)*100 told=tnew

disp table

NO if err<=epsilon_s YES print tnew

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Successive Approximation

START

Read function (g)

f=inline(g)

Read significant digits n

Input initial guess

epsilon_s= (0.5*10^(2-n)) epsilon_a=100 tr=fzero(inline (g))

disp tr

set abcd

disp abcd

set head

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

A disp head

while ea>=es

temp=t t=f(t)
ea=abs((t-temp)/t)*100 et=abs((tr-t)/tr)*100

disp table

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Gauss-Nave Elimination method


Start Input matrices A&B

[m,n]=size [A]

If m~=n YES For k=1:n-1

NO

Print Matrix must be square!

For i=k+1:n

Factor a(i,k)/a(k,k) 9i,k For j=k:n

a(I,j)=a(I,j)-factor a(k,j)

b(i)=b(i)- factor*b(k)

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering
M

Display A & B

For i=n:-1:1

x(i) = b(i) / a(I,i)

for j=1:i-1

b(j) = b(j) - x(i)*a(j,i)

Display values of x

End

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Gauss with Partial Pivoting method

Start

Input matrices A & B

[m,n] = size (a)

NO If m~=n

Print Matrix must be square!

YES E A For k=1:n-1

[xyz,i]=max(abs(a(k:n,k))) ipr=i+k-1;

if ipr~=k

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

B a([k,ipr],:)=a([ipr,k],:) b([k,ipr],:)=b([ipr,k],:) For i=k+1:n B

factor=a(i,k)/a(k,k)

For j=k:n

a(i,j)=a(i,j)-(factor*(a(k,j)))

b(i)=b(i)-factor*(b(k))

A Display A & B

for i=n:-1:1

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

x(i)=b(i)/a(i,i)

For j=1:i-1

b(j)=b(j)-x(i)*a(j,i) D Display x E End

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Thomas Algorithm


START

Input matix e,f,g,r

n=length (e)

for k=1:n

factor =e(k)/f(k) f(k+1)=f(k+1)-xg(k) r(k+1)= r(k+1)-factor*r(k)

x(n+1)=r(n+1)/f(n+1)

for k=n:1

x(k)=r(k)-g(k)*x(k+1)/k

display

end

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Gauss-Seidel without Relaxation method


Start

Input matrices A&B

[m,n]=size(a)

if (m~=n)

NO

Print Matrix Must Be Square!

YES For i= m:n

d(i)=b(i)/a(i,i)

d=d c=a

For i=1:n

For j=1:n

c(i,j)=a(i,j)/a(i,i)

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

c(i,i)=0 x(i)=0 B x = x

Display A, B, C &D

Input no. of iterations (p)

For k=i:p

For i=1:n

x(i)=d(i)-c(i,:)*x(:,1)

Display x

End

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Gauss-Seidel with Relaxation method START

Input matrix A(a) And B(b)

[m,n]=size (a) YES If m~=n NO For k = 1:n-1 E Print Matrix must be square!

[xyz,i]=max(abs(a(k:n,k))) Ipr=i+k-1

NO if ipr N=K

a[(k,ipr),:]=a[(ipr,k), :] b[(k,ipr),:]=b[(ipr,k), :]

for i=1:n

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Di = b(i)/a(1,i)

D=d; c=0

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

For i=1:n

For j=1:n

C(I,j)=a(I,j)/a(I,i)

C(I,i)=0 X(i)=0

X = X

Display a, b, c & d

Input weighing factor (lambda)

es=0.05 ea(i)=100 set head

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Display head

While ea(i)>=es

For i=1:n

Y=x(i) X(i)=d(i)-c(I,:)*x(:,1) X(i)=lambda*x(i)+(1_lambda)*y ea(i)=abs((x(i)-y)/x(i))*100

Set table

Display table

E end

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Flowchart: Least square techniques Linear fit.

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

START

Enter values of x & y in matrix form.

n = size of matrix x xy(1,1) = 0 i=1 X=0 Y=0 XY=0 While i< = n Xsqr=0

xy(1,i)=x(1,i)*y(1,i) xsqr(1,i)=x(1,i)^2

X=X+x(1,i); X=X+x(1,i); XY=XY+xy(1,i) Xsqr=Xsqr+xsqr(1,i) i = i +1

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Print x & y

a1=(n*XY-Y*X)/(n*Xsqr-X^2)

a0=(Y*Xsqr-X*XY)/(n*Xsqr-X^2)

ym=Y/n;

sr(1,1)=0; j=1;

While j< = n

sr(1,j)=(y(1,j)-a0-beta*x(1,j))^2

st(1,j)=(y(1,j)-ym)^2

j = j +1

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

xp=linspace(min(x),max(x)); yp=a0+a1*xp;

SR=sum(sr) ST=sum(st) r2=(ST-SR)/ST Print Function of best fitted line

Plot Function & best fitted curve

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Flowchart: Least square techniques Quadratic fit.

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

START

Enter values of x & y in matrix form.

n = size of matrix x xy(1,1) = 0

i=1 X=0 Y=0 XY=0 sx = sum(x) sy Xsqr=0 = sum(y) sx2 = sum(x.*x) sxy = sum(x.*y) sx2y = sum(x.*x.*y) sx3 = sum(x.*x.*x) sx4 = sum(x.*x.*x.*x)

a = [sx2 sx n; sx3 sx2 sx; sx4 sx3 sx2] b = [sy; sxy; sx2y]

z=inv(a)*b

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

a0 = z(1) a1 = z(2) a2 = z(3) Print Function of best fitted line

xp = linspace(min(x),max(x)) yp = z(3)*(xp.*xp)+z(2)*xp+z(1)

Plot Function & best fitted curve

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Flowchart: Least square techniques Power model.

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

START

Enter values of x & y in matrix form.

n = size of matrix x xy(1,1) = 0 y(1,1) = 0


i=1 X=0 Y=0 XY=0

While i< = n Xsqr=0

y(1,i)=log10(ya(1,i)) x(1,i)=log10(xa(1,i)) xy(1,i)=x(1,i)*y(1,i) xsqr(1,i)=x(1,i)^2

X=X+x(1,i); X=X+x(1,i); XY=XY+xy(1,i) Xsqr=Xsqr+xsqr(1,i)

i = i +1

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Print x & y

beta=(n*XY-Y*X)/(n*Xsqr-X^2)

a0=(Y*Xsqr-X*XY)/(n*Xsqr-X^2)

alpha=10^(a0) ym=Y/n;

sr(1,1)=0; j=1;

While j< = n

sr(1,j)=(y(1,j)-a0-beta*x(1,j))^2

st(1,j)=(y(1,j)-ym)^2

j = j +1

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

SR=sum(sr) ST=sum(st) r2=(ST-SR)/ST Print Function Of best fitted curve

xp = linspace(min(x),max(x)) yp = (xp.^beta)*alpha

Plot Function & best fitted curve

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Flowchart: Least square techniques Exponential fit.

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

START

Enter values of x & ya in matrix form.

n = size of matrix x xy(1,1) = 0 y(1,1) = 0 i=1 X=0 Y=0 XY=0 While i< = n Xsqr=0

y(1,i)=log(ya(1,i)) xy(1,i)=x(1,i)*y(1,i) xsqr(1,i)=x(1,i)^2 X=X+x(1,i); X=X+x(1,i); XY=XY+xy(1,i) Xsqr=Xsqr+xsqr(1,i) i = i +1

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Print x & ya

a1=(n*XY-Y*X)/(n*Xsqr-X^2)

a0=(Y*Xsqr-X*XY)/(n*Xsqr-X^2)

alpha=exp(a0) ym=Y/n;

sr(1,1)=0; j=1;

While j< = n

sr(1,j)=(y(1,j)-a0-beta*x(1,j))^2

st(1,j)=(y(1,j)-ym)^2

j = j +1

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

xp = linspace(min(x),max(x)) yp= alpha*exp(a1*xp)

SR=sum(sr) ST=sum(st) r2=(ST-SR)/ST Print Function Of best fitted curve

Plot Function & best fitted curve

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Flowchart: Lagrange interpolation.


START

Enter values of x & y in matrix form. & Enter value of u

n = length of matrix x p=1; s=0 dx = x(i+1) x(i)

For i = 1 to n

p = y(i)

For j = 1 to n

If i=~j

End

p=p*(u-x(j))/(x(i)-x(j))

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

s = s+p

Print Y

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Flowchart: N-G forward difference interpolation.


START

Enter values of x & y in matrix form & Enter value of X

n = size of matrix x d(1,1) = y(1,1)

dx = x(i+1) x(i)

For j = 1 to (n-1)

dy = y(j+1) y(j)

Display dy

d(j+1) = dy(1)

y = dy

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

alpha = (X-x(1))/dx(1)

a(1,1)=1 prod=1 For k = 1 to (n-2)

prod = prod * (alpha-k+1)

a(k+1) = prod

func = 0

For i = 1 to (n-1)

fx = a(i) * d(i) / (factorial(i-1))

func=func+fx

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Print Y

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Flowchart: N-G backward difference interpolation.


START

Enter values of x & y in matrix form. & Enter value of X

n = size of matrix x d(1,1) = y(n) dx = x(i+1) x(i)

newx(1,n:-1:1) = x(1,1:n)

newy(1,n:-1:1) = y(1,1:n)

For j = 1 to (n-1)

dy = y(j+1) y(j) B Display dy

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

B d(j+1) = dy(1)

y = dy

alpha = (x(n)-X)/dx(1)

a(1,1)=1 prod=1 For k = 1 to (n-2)

prod = prod * (alpha-k+1)

a(k+1) = prod

func = 0

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

For i = 1 to (n-1)

fx = a(i) * d(i) / (factorial(i-1))

func=func+fx

Print Y

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART:Hermite Interpolation
START

Accept values of x,f(x) and f(x) accept unknown x

n=size(x) suma=0 sumb=0 sum=0

for i=1 to n

pro=1 pro1=1

for j=1 to n

if ij

pro=pro*(xu-x(j))/(x(i)-x(j)) pro1=pro1*(x(i)-x(j))

L(I,1)=pro L(I,1)=pro1

for k=1 to n

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

suma=suma+(1-2*(xu-x(k))*dL(k))*((L(k))^2)*fx(k) A sumb=sumb+(xu-x(k))*((L(k))^2)*dfx(k)

sumf=suma+sumb

Display sumf

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Cubic Spline Interpolation

START

Accept pts. X1,y1 Accept unknown xg

m1=size(x1) n=m(1,2) x=x1 y=y1

plot curve(x,y)

assigning pts. In yy betwn x and y with 0:0.01:100 plot (x,y) with intermediate yy

defining f(x)=0 for end pts.

A(1,1:3)=[2*(x(3)-x(1)) (x(3)-x(2)) 0] B(1,1)=6*(y(3)-y(2))/(x(3)-x(2))-6*(y(2)-y(1))/(x(2)-x(1)) (solving for first row)

If n>3

else

For l=2:n-2

Solve Subsequent rows till n-2 by above method

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

D D

A(n-1,n-2:n-1)=[(x(n)-x(n-1)) 2*(x(n)-x(n-1))] B(n-1,1)=-6*(y(n)-y(n-1))/(x(n)-x(n-1)) (solving the last row)

Finding other values of f(x) By using gauss Solution for (A,B) Storing in N

For i=1 to n-1

M(i+1)=N(i) (assigning the values to f(x) from N)

Define function for cubic spline for each interval Ma/6/(xb-xa)*(xb-xx)^3-Mb/6/(xb-xa)*(xa-xx)^3+ (ya/(xb-xa)-Ma*(xb-xa)/6)*(xb-xx)-(yb/(xb-xa)-Mb*(xb-xa)/6)*(xa-xx)

xn(1:1000)=0 Yn(1:1000)=0

For i=1 to n-1

J=1 dx=(x(i+1)-x(i))/1000

for k=x(i):dx:x(i+1)

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

xn(j)=k; yn(j)=f(k,M(i),M(i+1),x(i),x(i+1),y(i),y(i+1)); j=j+1

C C

B B else if xg>=x(i) and xg<=x(i+1)

yg=f(xg,M(i),M(i+1),x(i),x(i+1),y(i),y(i+1))

plot (xn,yn)

display xg,yg

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Inverse Interpolation

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

START

Enter values of x & y in matrix form. & Enter value of r

n = size of matrix x p=1,s=0 for j=1 to n

for i= 1 to n

if i=j

numerator= r-y(i) denominator=y(j)-y(i) v(j)=numerator/denominator p=p*v(j) s=s+p*x(j) p=1

print s End

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Flowchart: Newton Forward differentiation.


START

Enter values of x & y in matrix form. & Enter value of X

n = size of matrix x p=1 h = x(i+1) x(i)

For j = 1 to n

If r=~x(j)

End

p=j

d(1,1)=y(1,p)

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

For j = 1 to (n-p)

dy = y(j+1) y(j)

Display dy

y = dy

d(j+1)=y(1,p)

f=0

For k = 1 to (n-1)

fr=d(k+1)/k

f=f+((-1)^(k-1))*fr

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

dx=(f/h(1))

Print dx

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Flowchart: Newton Backward differentiation.

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

START

Enter values of x & y in matrix form. & Enter value of X n = size of matrix x p=1 h = x(i+1) x(i)

x(1,n:-1:1)=xin(1,1:n); y(1,n:-1:1)=yin(1,1:n)

For j = 1 to n

If r=~x(j)

End

p=j

d(1,1)=y(1,p)

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

For j = 1 to (n-p)

dy = y(j+1) y(j)

Display dy

y = dy*(-1)

d(j+1)=y(1,p)

f=0

For k = 1 to (n-1)

fr=d(k+1)/k

f=f+fr

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

dx=(f/h(1))

Print dx

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Trapezoidal (single segment) method

START

print statement

Read function(x) Input lower limit(a) Upper limit(b)

y=inline(x) h=(b-a)/n s=0,n=1

for i=1:n-1

t=2*y(a+i*h) s=s+t

A=h*(y(a)+y(b)+s)

print A

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Trapezoidal(multiple segment) method

START

print statement

Read function(x) Input lower limit(a) Upper limit(b) No. of divisions(n)

y=inline(x) h=(b-a)/n s=0

for i=1:n-1

t=2*y(a+i*h) s=s+t

A=h*(y(a)+y(b)+s)

print A

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Simpsons One Third Rule(single segment) for Integration

Start

print statement

Read function(x) & input lower limit(a) & input upper limit (b)

y=inline (x) h=(b-a)/n s=0,n=2

for i=1:n-1

if mod(I,2)==1

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

NO t=2*y(a+i*h) YES

t= 4*y(a+i*h)

s=s+t

A=h*(y(a)+y(b)+s)/3

print A

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Simpsons One Third Rule(multiple segment) for Integration


Start

print statement

Read function(x) & input lower limit(a) & input upper limit (b) & no. of divisions n

while mod(n,2)~=0

input no. of division divisible by 2

y=inline (x) h=(b-a)/n s=0

for i=1:n-1

if mod(I,2)==1

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering
M

NO t=2*y(a+i*h) YES

t= 4*y(a+i*h)

s=s+t

A=h*(y(a)+y(b)+s)/3

print A

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Simpsons Three Eight Rule for Integration


Start

print statement

Read function(x) & input lower limit(a) & input upper limit (b) & no. of divisions n

while mod(n,3)~=0

input no. of division divisible by 3

y=inline (x) h=(b-a)/n s=0

for i=1:n-1

if mod(I,3)==1

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering
M

NO t=2*y(a+i*h) YES

t= 4*y(a+i*h)

s=s+t

A=3*h*(y(a)+y(b)+s)/8

print A

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Gauss-Legendre 2-pt method

Start

Print NUMERICAL INTEGRATION BY GAUSS LEGEDRE 2-POINT FORMULA

Read function (x) Input lower limit a & upper limit b

f=inline(x) c=(b-a)/2 d=(b+a)/2 x1=c/sqrt(3)+d x2=-c/sqrt(3)+d y1=f(x1) y2=f(x2) A=(y1+y2)*c

Print A

End

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Gauss-Legendre 3-pt method

Start

Print NUMERICAL INTEGRATION BY GAUSS LEGEDREN 3-POINT FORMULA

Read function (x) Input lower limit a & upper limit b

f=inline(x) c=(b-a)/2 d=(b+a)/2 x1=c/sqrt(3)+d x2=-c/sqrt(3)+d x3=d y1=f(x1) y2=f(x2) y=f(x3) A=(5/9*y1+5/9*y2+8/9*y3)*c

Print A

End

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Double Integration by trapezoidal method

START

print statement

Read function(xy) Input lower limit of x(ax) Input lower limit of y(ay) Upper limit of x(bx) Upper limit of y(by)

Input nx Input ny

while mod(nx,2)~=0

input no. of intervals wrt x(nx) no. of intervals wrt (ny)

f= inline(xy)

h=(bx-ax)/nx k=(by-ay)/ny an=0

for i=0:nx-1

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

A for j=0:ny-1

tr=f(ax+i*h,ay+j*k)+f(ax+i*h,ay+(j+1)*k)+f(ax+(i+1)*h,ay+(j+1)*k)+f(ax+(i+1)*h,ay+ j*k) an=an+tr;

A=h*k*an/4

B Print A

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Double Integration by Simpsons One Third method

START

print statement

Read function(xy) Input lower limit of x(ax) Input lower limit of y(ay) Upper limit of x(bx) Upper limit of y(by)

nx=3 ny=3

while mod(nx,2)~=0

input no. of intervals wrt x(nx) no. of intervals wrt (ny)

f= inline(xy)

h=(bx-ax)/nx k=(by-ay)/ny an=0

for i=0:2:nx-1

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

A for j=0:2:ny-1

tr1=f(ax+i*h,ay+j*k)+f(ax+i*h,ay+(j+2)*k)+f(ax+(i+2)*h,ay+(j+2)*k)+f(ax+(i+2)* h,ay+j*k) tr2=f(ax+i*h,ay+(j+1)*k)+f(ax+(i+1)*h,ay+(j+2)*k)+f(ax+(i+2)*h,ay+(j+1)*k)+f(a x+(i+1)*h,ay+j*k) tr3=f(ax+(i+1)*h,ay+(j+1)*k) an=an+tr1+4*tr2+16*tr3

A=h*k/9*an

B Print A

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Euler method

START

Accept the function f(x,y) Get inputs of initial values x0,y0 Get step size h and unknown xu n= (xu-x0)/h

for i=1 to n

x1=x0+h y1=y0+h*f(x0,y0) x0=x1 y0=y1

Displaying y1

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART:Heuns method

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

START

Accept function Accept x0,y0,h and unknown x

n = (x-x0)/h

for i= 1 to n

yf = y0 + h*f(x0,y0) yff = y0 + h*(f(x0,y0) + f(x0+h,yf))/2

y0 = yff; x0 = x0 + h

display yff

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Modified Euler method

START

Accept the function f(x,y) Get inputs of initial values x0,y0 n=2 and unknown xu Get accuracy h= (xu-x0)/n

for i=1 to n

x1=x0+h y1=y0+h*f(x0,y0) ynew=y0+(h/2)*(f(x0,y0)+f(x1,y1) diff=|y1-ynew|

while diff>accuracy A y1=ynew ynew= y0+(h/2)*(f(x0,y0)+f(x1,y1) diff=|y1-ynew|

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

x0=x1 A y0=ynew

Displaying y1

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Runge-Kutta2nd order method

START

Accept the function f(x,y) Get inputs of initial values x,y Get step size h and unknown xu n= (xu-x0)/h m =size(x)

for i=1 to n

ya=y3+(h/24) (-9*f(x0,y0) +37*f(x1,y1)- 59*f(x2,y2) +55*f(x3,y3)) ya1=y3+ (h/24)(f(x1,y1)-5*f(x2,y2)+19*f(x3,y3)+9*f(x3+h,ya)

while ya1=!ya

ya=ya1 ya1=y3+(h/24)*(f(x1,y1)-5*f(x2,y2)+19*f(x3,y3)+ 9*f(x3+h,ya);

for i=1: m A x(j)=x(j+1); y(j)=y(j+1);

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

x(m)=x(m)+h y3=ya1 A

Displaying yanswer

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Runge-Kutta 4th order method

START

Accept the function f(x,y) Get inputs of initial values x0,y0 Get step size h and unknown xu n= (xu-x0)/h

for i=1 to n

x1=x0+h y1=y0+h*f(x0,y0) x0=x1 y0=y1

c1=h*s(x0,y0); c2=h*s((x0+(h/2)),(y0+(c1/2))); c3=h*s((x0+(h/2)),(y0+(c2/2))); c4=h*s(x1,(y0+c3)); c=(c1+2*c2+2*c3+c4)/6;

yanswer=y0+c y0=yanswer x0=x1 B

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

B A

Displaying yanswer

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Milnes prediction-correction order method

START

Accept the function f(x,y) Get inputs of initial values (x0,y0), (x1,y1), (x2,y2), (x3,y3) Get step size h and unknown xu n= (xu-x0)/h

f1=f(x1,y1),f2=f(x2,y2), f3=f(x3,y3)

for i=1 to n

y4pr=y(1)+(4*h/3)*(2*f1-f2+2*f3); f4pr=f(xu-h*(n-i),y4pr); y4cr=y(3)+(h/3)*(f2+4*f3+f4pr)

if y4prp4cr

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

y4pr=y4cr y4=y4cr

f4=f(xu-h*(n-i),y4) f1=f2;f2=f3;f3=f4 y0=y1 y2=y3 yans=y4cr

yanswer=y0+c y0=yanswer x0=x1

Displaying yans

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Runge-Kutta for simultaneous equation

START

Accept the function f(x,y,z) Accept the function g(x,y,z) Get inputs of initial values x0,y0,z0 Get step size h and unknown xu n= (xu-x0)/h

for i=1 to n

x1=x0+h y1=y0+h*f(x0,y0) x0=x1 y0=y1

c1=h*f(x0,y0,z0) d1=h*g(x0,y0,z0) c2=h*g((x0+(h/2)),(y0+(c1/2)),(z0+(d1/2))) d2=h*sg((x0+(h/2)),(y0+(c1/2)),(z0+(d1/2))) c3=h*f((x0+(h/2)),(y0+(c2/2)),(z0+(d2/2))) d3=h*g((x0+(h/2)),(y0+(c2/2)),(z0+(d2/2))) c4=h*f(x1,(y0+c3),(z0+d3)) d4=h*g (x1,(y0+c3),(z0+d3))

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

c=(c1+2*c2+2*c3+c4)/6 d=(d1+2*d2+2*d3+d4)/6 yans=y0+c zans=z0+d

y0=yans A z0=zans x0=x1

Displaying yans Displaying zans

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART: Adam Bashforth order method

START

Accept the function f(x,y) Get inputs of initial values x,y Get step size h and unknown xu n= (xu-x0)/h m =size(x)

for i=1 to n

ya=y3+(h/24) (-9*f(x0,y0) +37*f(x1,y1)- 59*f(x2,y2) +55*f(x3,y3)) ya1=y3+ (h/24)(f(x1,y1)-5*f(x2,y2)+19*f(x3,y3)+9*f(x3+h,ya)

while ya1=!ya

ya=ya1 ya1=y3+(h/24)*(f(x1,y1)-5*f(x2,y2)+19*f(x3,y3)+ 9*f(x3+h,ya);

for i=1: m A x(j)=x(j+1); y(j)=y(j+1);

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

x(m)=x(m)+h y3=ya1 A

Displaying yanswer

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART:PARABOLIC EQUATION

START

accept x0,xn,t0,tn accept h,k,C accept boundary value (b) define function u

x=x0:h:xf t=t0:k:tf x=x t=t n=(xf-x0)/h m=(tf-t0)/k

Define matrix A(:,1)=b A(:,n)=b r=k/h^2*C^2

for i=2 to n

A(m+1,i)=u(x(i))

for j=m:-1:1

for i=2 to n

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

A(j,i)=r*A(j+1,i-1)+(1-2*r)*A(j+1,i)+r*A(j+1,i+1)

Display A

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART:Crank-Nicolson

START

`Accept xi,xf,h,ti,tf,k Accept function Accept value of c

co=(xf-xi)/h ro=(tf-ti)/k r=k*C^2/h^2 a=1,b=1.c=1

u(1,2:co+2)=xi:h:xf u(2:ro+2,1)=ti:k:t

Accept const values for: u(2:ro+2,2) u(2:ro+2,2)

for i=3 to co+1

u(2,i)=f(u(1,i))

ui=u; k=1

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

B B

while c==1 and k<=1000

ui=u;

for i=2 to ro+1

for j=3 to co+1

u(i+1,j)=1/4*(u(i+1,j-1)+u(i+1,j+1)+u(i,j-1)+u(i,j+1))

k=k+1 uf=(u-ui)./u

if max(max(uf))<=0.001

c=0

display u

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART:Hyperbolic Equation

Start

Input Initial value of x (xi) Final value of x (xf) Step size for x (h)

co=(xf-xi)*10000/(h*10000)

Input Initial value of t (ti) Final value of t (tf) Step size for t (k)

ro=(tf-ti)*10000/(k*10000)

Input all values of u(x) (s)

r=h/k

If r~=C

YES

error

NO A

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

u(1,2:co+2)=xi:h:xf u(2:ro+2,1)=ti:k:tf

Input Constant value for xi (u(2:ro+2,2)) Input Constant value for xf (u(2:ro+2,co+2)

For i=3:co+1

u(2,i)=f(u(1,i))

For i=3:co+1

u(3,i)=(u(2,i-1)+u(2,i+1))/2

For i=4:ro+2

For j=3:co+1

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

u(i,j)=u(i-1,j-1)+u(i-1,j+1)-u(i-2,j)

B A Display u

End

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

FLOWCHART:Elliptical Equation
Start

Input Temperature of Upper surface (u) Left surface (l) Right surface (r) Lower surface (b) No. of elements in a row (cs)

n=cs-1 an(n,n)=0

For i=1:n^2

For j=i:n^2

YES
IF i==j

an(i,j)=4

NO M YES
(i,j)=-1

IF mod(i,n)==1 && j==i+1

M NO C

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

IF j==i-n && j>0

YES

an(i,j)=-1

NO

IF j==i+n && j<=n^2

YES

an(i,j)=-1

NO

mod(i,n)==0 && j==i-1

YES

an(i,j)=-1

NO

IF mod(i,n)>1 && j==i+1 || j==i-1 )

NO B

YES
an(i,j)=-1

B A
so(n)=0

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

For i=1:n^2

YES
If i==1

so(i)=u+l

NO

N YES

If i>1 && i<n

so(i)=u

NO

N YES

If i==n

so(i)=u+r

NO

If mod(i,n)==1 && i>n && i<=n^2-n

YES

so(i)=l

NO

mod(i,n)>1 && mod(i,n)<n && i>n && i<=n^2-n

YES
so(i)=0

NO F

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

If mod(i,n)==0 && i>n && i<=n^2-n

YES
so(i)=r

NO YES

If i==n^2n+1

so(i)=l+b

NO

If i>n^2-n+1 && i<n^2

YES

so(i)=b

NO

If i==n^2

NO E

YES
so(i)=b+r

N E Display an & so

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

an1=an clear an t=GaussSoln(an1,so,n^2) k=1

For i=1:n

For j=1:n

t1(i,j)=t(k) k=k+1

Display t1

For i=1:n

For j=1:n

scatter(i,j,80,[0.5 0 0],'filled') s=sprintf('\n %1.2f',(t1(i,j))) text(j,i,s)

axis ij axis ([ 0 n+1 0 n+1])

End

Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12 MITCOE Mechanical Engineering

Potrebbero piacerti anche