Like this document? Why not share!

# MITCOE 2011-12 conm-submission

## on Oct 21, 2011

• 496 views

### Views

Total Views
496
Views on SlideShare
496
Embed Views
0

Likes
0
5
0

No embeds

### Report content

• Comment goes here.
Are you sure you want to

## MITCOE 2011-12 conm-submissionDocument Transcript

• Assignment No: 1Statement: Write down the Matlab Program using Newton-Raphson method for any equation.Solution:Input :clc; clear all; % Clears the workspaceformat long;% Take i/p from usera=input(n Enter the function : ,s);b=input(n Enter the derivative of function : ,s);% Converts the input string into symbolic functionft=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 tttttepsilon_a tttttepsilon_ttttttepsilon_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 ttt %ftttt %4.9f ttt %f ttt%f,tnew,epsilon_a,epsilon_t,epsilon_s); disp(table);end% Prints the answerfprintf(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 : (exp(t))*cos(t)-(exp(t))*sin(t)enter no. of significant digits: 4 0.433560875352657BY NEWTON-RAPHSON METHOD:Enter your initial guess for root: 0Time epsilon_a epsilon_t epsilon_s4.000000e-001 100.000000 7.740752743 0.0050004.327044e-001 7.558146 0.197537266 0.0050004.335602e-001 0.197392 0.000145353 0.0050004.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: 2Statement: Write down the Matlab Program using Modified Newton-Raphson method for anyequation.Solution:Input :clc; Clear all; % Clears the workspace% Takes the i/p from usera=input(n Enter the function : ,s);b=input(n Enter the derivative of function : ,s);c=input(n Enter second order derivative : ,s);x0=0;% Converts the input string into symbolic functionfx=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 tttttepsilon_a tttttepsilon_ttttttepsilon_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 ttt %ftttt %4.9f ttt %f ttt%f,tnew,err,epsilon_t,epsilon_s); disp(table); if (err<=epsilon_s) break; endendfprintf(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 : 6BY MODIFIED NEWTON-RAPHSON METHOD:Time epsilon_a epsilon_t epsilon_s6.117645e+000 1.923040 318.613323399 0.0005006.121248e+000 0.058870 318.742096720 0.0005006.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: 3Statement: Write down the Matlab Program using successive approximation method for anyequation.Solution:Input :clc;clear;g=input(Enter the function:,s);f=inline(g); % Defining functionn=input(Enter number of significant digits: );es=(0.5*10^(2-n)); % Stopping criteriaea=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 tttttepsilon_a ttttt epsilon_t tttttepsilon_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 ttt %ftttt %4.9f ttt %f ttt%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: 2Enter initial guess: 0BY SUCCESSIVE APPROXIMATION METHOD: 0.567143290409784 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 4Statement: Write down the Matlab Program using Gauss Naïve 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 eliminationfor 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 matrixA. end b(i)=b(i)-factor*(b(k)); % calculates each element of matrix B. end disp (a);enddisp (a);disp (b);% backward substitutionfor 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); endenddisp(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 -1 1 3 4 2 2 1 1enter column matrix B[]: [697]b = 6 9 7 1.0000 -1.0000 1.0000 0 7.0000 -1.0000 0 0 -0.5714 6.0000 -9.0000 -1.1429VALUES ARE: 3.0000 -1.0000 2.0000 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 5Statement: 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%pivotingfor 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 matrixA. end b(i)=b(i)-factor*(b(k)); % calculates each element of matrix B. end disp (a);end%disp (a);disp (b);% backward substitutionfor 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); endenddisp(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 1.00000000000000 -2.00000000000000 0 -1.37500000000000 7.75000000000000 0 -5.75000000000000 -1.50000000000000 -8.00000000000000 1.00000000000000 -2.00000000000000 0 -5.75000000000000 -1.50000000000000 0 0 8.10869565217391 -20.00000000000000 -43.00000000000000 -16.21739130434783VALUES ARE: 4 8 -2 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 6Statement: 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 efor 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 vectorendx(n+1)=r(n+1)/f(n+1); % Transforming unknown vectorfor k=n:-1:1 x(k)=(r(k)-g(k)*x(k+1))/f(k); % Finding values of unknownsenddisp(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: 7Statement: 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!); endfor i=1:n d(i)=b(i)/a(i,i);endd=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;endx=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 EngineeringComputer 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.20000000000000 0.10000000000000 7.00000000000000 -0.30000000000000 0.30000000000000 -0.20000000000000 10.00000000000000 7.85000000000000 -19.30000000000000 71.40000000000001 0 -0.03333333333333 -0.06666666666667 0.01428571428571 0 -0.04285714285714 0.03000000000000 -0.02000000000000 0Enter 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 EngineeringComputer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 8Statement: 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%pivotingfor 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. endendfor i=1:n d(i)=b(i)/a(i,i);endd=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;endx=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(tttttttttValue of x ttttttttttttttValueof 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 ttt %ftttt %4.9f ttt%f ttt%ftttt %4.9f,x,ea);disp(table1);endOutput :enter matrix A[]: [-3 1 12;6 -1 -1;6 9 1]enter column matrix B[]: [50;3;40] 6 -1 -1 6 9 1 -3 1 12 3 40 50 0.5000 4.4444 4.1667 0 -0.1667 -0.1667 0.6667 0 0.1111 -0.2500 0.0833 0Enter 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 ea4.750000e-001 3.921389 3.760702546 100.000000 100.000000 100.0000000001.715081e+000 2.935111 4.321337313 72.304517 33.602766 12.9736404711.709692e+000 2.830032 4.356407772 0.315233 3.712986 0.8050315981.698338e+000 2.828267 4.355604413 0.668543 0.062401 0.018444248 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 42Statement: Write down the Matlab Program to fit curve y = a0 + a1*x by using least squaretechniques 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;enddisp(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;endSR=sum(sr);ST=sum(st);r2=(ST-SR)/STs=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 2.5000 2.0000 4.0000 3.5000 6.0000 5.5000r2 = 0.8683Best fit curve (straight line) for above data is given by : y = 0.839286 * x + 0.071429 y=a0+a1*x 6 5 4 values of y 3 2 1 0 1 2 3 4 5 6 7 values of x Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 9Statement: Write down the Matlab Program to fit curve y = a0 + a1*x+a2x2 by using least squaretechniques 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 0.5 1 1.5 2 2.5 Values of x Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 10Statement: Write down the Matlab Program to fit curve y = a1*(xb1) by using least squaretechniques 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)); % To calculate log of y x(1,i)=log10(xa(1,i)); % To calculate log of x 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;enddisp(xa);disp(ya)beta=(n*XY-Y*X)/(n*Xsqr-X^2);a0=(Y*Xsqr-X*XY)/(n*Xsqr-X^2);alpha=10^(a0); % To calculate co-eff of x^a0ym=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 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• st(1,j)=(y(1,j)-ym)^2; % To calculate st for each x j=j+1;endSR=sum(sr);ST=sum(st);r2=(ST-SR)/STs=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 onplot(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 93.3300 148.8900 315.5600 1.3500 0.0850 0.0120 0.0008r2 = 0.9757Best fit curve (polynomial) for above data is given by :y = 38147.936083 * x^(-3.013376) Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• y=alpha*(x)b 2 1.8 1.6 1.4 1.2values of y 1 0.8 0.6 0.4 0.2 0 0 50 100 150 200 250 300 350 values of x Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 41Statement: Write down the Matlab Program to fit curve y = a1 * e (b1*x) by using least squaretechniques 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 xxy(1,1)=0; y(1,1)=0; % Defining matrix xy & yi=1; X=0; Y=0; XY=0; Xsqr=0; % Setting initial condition for loopwhile i<=n; 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;enddisp(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; % Finding meansr(1,1)=0;j=1;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; Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• endxp = linspace(min(x),max(x)); % Condition for graphyp= alpha*exp(a1*xp); % Given functionSR=sum(sr);ST=sum(st);r2=(ST-SR)/ST % Co-efficient of determinations=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) % Plots function & best fitted curve simultaneouslygrid on;xlabel(values of x); % Defining specifications of graphylabel(values of y);title(y=alpha*e^(beta*x));grid on; % To display grid on graphOutput :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 0.8000 1.2000 1.6000 2.0000 2.3000 800 975 1500 1950 2900 3600r2 = 0.9933Best 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 3000values of y 2500 2000 1500 1000 500 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 2.2 2.4 values of x Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 11Statement: 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 xp=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; % Summationendfprintf(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.2Value of y at required x is : 490.360287 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 12Statement: Write down the Matlab Program for Newton-Gregory Forward DifferenceInterpolation.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); % Spatial diff.(for equally spaced data)d(1,1)=y(1,1);disp(x);disp(y);for j=1:(n-1) dy=diff(y); % Delta matrix disp(dy); d(j+1)=dy(1); % Stores 1st value of delta matrix. y=dy;endalpha=(X-x(1))/dx(1); % Value of alpha is calculated.a(1,1)=1; prod=1;for k=1:(n-2) prod=prod*(alpha-k+1); a(k+1)=prod;endfunc=0;for i=1:n-1 fx=a(i)*d(i)/(factorial(i-1)); func=func+fx;end 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 3 4 5 6 7 8 9 19 48 99 178 291 444 643 894 29 51 79 113 153 199 251 22 28 34 40 46 52 6 6 6 6 6 0 0 0 0 0 0 0 0 0 0Value of function calculated by N-G forward interpolation : 70.375000 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 13Statement: Write down the Matlab Program for Newton-Gregory Backward DifferenceInterpolation.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); % Spatial diff.(for equally spaced data)d(1,1)=y(n);newx(1,n:-1:1)=x(1,1:n); % Reversing order of matrix x so that nth value isbrought 1st.newy(1,n:-1:1)=y(1,1:n); % Reversing order of matrix y so that nth value isbrought 1st.disp(newx)disp(newy)for j=1:(n-1) dy=diff(newy); % Delta matrix disp(dy); d(j+1)=dy(1); % Stores 1st value of delta matrix. newy=dy;endalpha=(x(n)-X)/dx(1); % Value of alpha is calculated.a(1,1)=1; prod=1;for k=1:(n-2) prod=prod*(alpha-k+1); a(k+1)=prod;end 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;ends=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 0.4000 0.3000 0.2000 0.1000 2.2800 2.0000 1.7600 1.5600 1.4000 -0.2800 -0.2400 -0.2000 -0.1600 0.0400 0.0400 0.0400 1.0e-015*0.2220 -0.2220 -4.4409e-016Value 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: 14Statement: 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 matrixsum=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 termendfor 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);endsumf=suma+sumb;disp(The value of fx at unknown x is: );disp(sumf);Output:HERMITE INTERPOLATIONEnter the values of x: [0;1]Enter the value unknown of x: 0.4Enter 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 EngineeringComputer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 15Statement: Write down the Matlab Program for interpolation by Cubic spline.Solution:Input :% Clearing Workspaceclear all;clc;close;% Defining Input pointsx1=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 Interpolationyy = 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 pointM(1:n+1)=0;% First row of matrix to be solvedA(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-2if 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)); endend% Last 1 rowA(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 Mfor i=1:n-1 M(i+1)=N(i);end% Creating the interpolation function between intervalsf=inline(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),xx,Ma,Mb,xa,xb,ya,yb);% Ploting the spline in intervalsxn(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)]);endhold off;fprintf(@x=%f, y=%fn,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 endendSoln=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 EngineeringComputer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 16Statement: 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;endfprintf(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: 17Statement: 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 sizedisp(x);disp(y);for j=1:n if (r==x(j)) p=j; endendd(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.endf=0;for k=1:n-1 fr=d(k+1)/k; f=f+((-1)^(k-1))*fr;enddx=(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 0 0 0Value of dy/dx at 1.500000 is : 4.750000 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 18Statement: 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 sizey(1,n:-1:1)=yin(1,1:n); % Reversing order of matrix y so that nth value is brought1st.x(1,n:-1:1)=xin(1,1:n); % Reversing order of matrix x so that nth value is brought1st.disp(x)disp(y)for j=1:n if (r==x(j)) p=j; endendd(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);endf=0;for k=1:n-1 fr=d(k+1)/k; f=f+fr;enddx=(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-016Value of dy/dx at 40.000000 is : -0.011317 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 19Statement: Write down the Matlab Program using Trapezoidal rule(single segment) for anyfunction.Solution:Input :clear;clc;fprintf(NUMERICAL INTEGRATION BY TRAPEZOIDAL RULE nn);x=input(Enter a function to integrate f(x)=,s);a=input(Enter Lower Limit: );b=input(Enter Upper Limit: );n=2; % No. of pointsy=inline(x); % Defining functionh=(b-a)/n; % Step sizeS=0;for i=1:n-1; t=2*y(a+i*h); S=S+t;endA=h/2*(y(a)+y(b)+S); % Calculation of areafprintf(nAnswer= %fn,A);Output :NUMERICAL INTEGRATION BY TRAPEZOIDAL RULEEnter a function to integrate f(x)=4*x+2Enter Lower Limit: 1Enter Upper Limit: 4Answer= 36.000000 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 20Statement: Write down the Matlab Program using Trapezoidal rule(multiple segment) for anyfunction.Solution:Input :clear;clc;fprintf(NUMERICAL INTEGRATION BY TRAPEZOIDAL RULE nn);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 functionh=(b-a)/n; % Step sizeS=0;for i=1:n-1; t=2*y(a+i*h); S=S+t;endA=h/2*(y(a)+y(b)+S); % Calculation of areafprintf(nAnswer= %fn,A); Output :NUMERICAL INTEGRATION BY TRAPEZOIDAL RULEEnter a function to integrate f(x)=4*x+2Enter Lower Limit: 1Enter Upper Limit: 4enter no. of segments6Answer= 36.000000 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Third Year Mechanical EngineeringComputer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 21Statement: Write down the Matlab Program using Simpson’s 1/3rd (single segment) rule for anyfunction.Solution:Input :clear;clc;fprintf(NUMERICAL INTEGRATION BY SIMPSONS 1/3 RULE nn);x=input(Enter a function to integrate f(x)=,s);a=input(Enter Lower Limit: );b=input(Enter Upper Limit: );n=2; % No. of segmenty=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;endA=h/3*(y(a)+y(b)+S);fprintf(nAnswer= %fn,A);Output :NUMERICAL INTEGRATION BY SIMPSONS 1/3 RULEEnter a function to integrate f(x)=exp(x)Enter Lower Limit: 0Enter Upper Limit: 4Answer= 44.247402 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 22Statement: Write down the Matlab Program using Simpson’s 1/3rd (multiple segment) rule forany function.Solution:Input :clear;clc;fprintf(NUMERICAL INTEGRATION BY SIMPSONS 1/3 RULE nn);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;endA=h/3*(y(a)+y(b)+S);fprintf(nAnswer= %fn,A); Output :NUMERICAL INTEGRATION BY SIMPSONS 1/3 RULEEnter a function to integrate f(x)=exp(x)Enter Lower Limit: 0Enter Upper Limit: 4enter no.of divisions:5Answer= 44.683772 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 23Statement: Write down the Matlab Program using Simpson’s 3/8th rule for any function.Solution:Input :clear;clc;fprintf(NUMERICAL INTEGRATION BY SIMPSONS 3/8 RULE nn);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]: );endy=inline(x); % Defining functionh=(b-a)/n; % Step sizeS=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;endA=3*h/8*(y(a)+y(b)+S); % Area calculationfprintf(nAnswer= %fn,A);Output :Enter the function: 4*x-1Initial Value of x :1Final Value of x :4Enter No. of Divisions [Should be divisible by 3]: 3Answer: 27.000000>> Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 24Statement: Write down the Matlab Program for Combined Simpson’s Rule.Solution:Input :clear;clc;j=1;fprintf(NUMERICAL INTEGRATION BY MULTIPLE SIMPSONS RULE nn);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]: ); % Conditionfor no. of segments if mod(n-3,2)==0 j=0; endendy=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);endS=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;endA=A+h/3*(y(a+3*h)+y(b)+S);fprintf(nAnswer= %fn,A); Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• OUTPUT:NUMERICAL INTEGRATION BY MULTIPLE SIMPSONS RULEEnter a function to integrate f(x)=x^0.1*(1.2-x)*(1-exp(20*(x-1)))Enter Lower Limit: 0Enter Upper Limit: 2Enter No. of Divisions [(n-3) divisible by 2]: 5Answer= 55501691.391968>> Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 25Statement: Write down the Matlab Program for Gauss-Legendre 2-pt method.Solution:Input :clear;clc;fprintf(NUMERICAL INTEGRATION BY GAUSS-LEGENDRE 2-POINT FORMULA nn);x=input(Enter a function to integrate f(x)=,s);a=input(Enter Lower Limit: );b=input(Enter Upper Limit: );f=inline(x); % Defining functionc=(b-a)/2; % Constantsd=(b+a)/2; % Constantsx1=c/sqrt(3)+d;x2=-c/sqrt(3)+d;y1=f(x1);y2=f(x2);A=(y1+y2)*c;fprintf(nAnswer= %fn,A);OUTPUT:NUMERICAL INTEGRATION BY GAUSS-LEGENDRE 2-POINT FORMULAEnter a function to integrate f(x)=x^3+x-1Enter Lower Limit: 1Enter Upper Limit: 4Answer= 68.250000 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 26Statement: 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 nn);x=input(Enter a function to integrate f(x)=,s);a=input(Enter Lower Limit: );b=input(Enter Upper Limit: );f=inline(x); % Defining functionc=(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= %fn,A);Output :NUMERICAL INTEGRATION BY GAUSS-LEGENDRE 3-POINT FORMULAEnter a function to integrate f(x)=x^2-5*x+2Enter Lower Limit: 3Enter Upper Limit: 5Answer= -3.333333 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Third Year Mechanical EngineeringComputer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 27Statement: Write down the Matlab Program using Double integration by trapezoidal rule forany function.Solution:Input :clear;clc;% Taking Inputfprintf(DOUBLE INTEGRATION BY TRAPEZOIDAL RULE nn);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 functionf=inline(xy);% Main Calculationsh=(bx-ax)/nx;k=(by-ay)/ny;an=0;for i=0:nx-1 for j=0:ny-1tr=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; endendA=h*k/4*an;fprintf(nAnswer= %fn,A); Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Output :DOUBLE INTEGRATION BY TRAPEZOIDAL RULEEnter a function to integrate f(x,y)=x+yEnter Lower Limit of x: 0Enter Upper Limit of x: 2Enter Lower Limit of y: 1Enter Upper Limit of y: 3No. of intervals for integration w.r.t. x: 2No. of intervals for integration w.r.t. y: 2Answer= 12.000000 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 28Statement: Write down the Matlab Program using double integration by Simpson’s 1/3rd rulefor any function.Solution:Input :clear;clc;% Taking Inputfprintf(DOUBLE INTEGRATION BY SIMPSONS 1/3rd RULE nn);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 functionf=inline(xy);% Main Calculationsh=(bx-ax)/nx;k=(by-ay)/ny;an=0;for i=0:2:nx-1 for j=0:2:ny-1tr1=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; endendA=h*k/9*an;fprintf(nAnswer= %fn,A); Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Output :DOUBLE INTEGRATION BY SIMPSONS 1/3rd RULEEnter a function to integrate f(x,y)=x-y+1Enter Lower Limit of x: 6Enter Upper Limit of x: 14Enter Lower Limit of y: 1Enter Upper Limit of y: 5No. of intervals for integration w.r.t. x (Should be even): 4No. of intervals for integration w.r.t. y (Should be even): 4Answer= 256.000000 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 29Statement: 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 functionn=(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;ends=sprintf(n Value of y At x = %f Is : %f,xf,y(n));disp(s);Output :Enter A Function dy/dx : (x+y)/((y^2)-(sqrt(x*y)))Enter The Initial Value of x :1.3Enter The Initial Value of y :2Enter Value of "x" At Which Value of "y" Is To Be Found: 1.8Enter 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: 30Statement: Write down the Matlab Program for Heun’s 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);endOutput :HEUNS METHODEnter The Function dy/dx : 4*exp(.8*x) - .5*yEnter The Initial Value of x: 0Enter Initial Value of y: 2Enter step size: 1Enter Value of x For Which y Is To Be Found: 4Value y = 6.701082 At x1Value y = 16.319782 At x2Value y = 37.199249 At x3Value y = 83.337767 At x4 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Third Year Mechanical EngineeringComputer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 31Statement: Write down the Matlab Program for Modified Euler method.Solution:Input:clc; clear all; % Clears the workspacedisp(MODIFIED EULER METHOD);format long% Take the input from usereq=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: );% Calculatoinsh=(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 answerdisp(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 METHODEnter the diff. eqn in x and y: sqrt(x+y)Enter y: 2.2Enter x: 1Enter unknown x: 1.2Enter 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: 32Statement: Write down the Matlab Program for Runge-Kutta 2nd order method.Solution:Input:clc; clear all; % Clears the workspacedisp(RUNGE KUTTA METHOD 2ND ORDER);format long% Takes the input from usereq=input(Enter the diff. eqn in x and y: ,s);s=inline(eq); % Converts the i/p string into symbolic functiony0=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 answerdisp(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 EngineeringComputer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Output:RUNGE KUTTA METHOD 2ND ORDEREnter the diff. eqn in x and y: -(y+x*y^2)Enter y: 1Enter x: 0Enter unknown x: 0.3Enter step size: 0.1The 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: 33Statement: Write down the Matlab Program for Runge-Kutta 4th order method.Solution:Input:clc; clear all; % Clears the workspacedisp(RUNGE KUTTA METHOD 4TH ORDER);format long% Takes the input from usereq=input(Enter the diff. eqn in x and y: ,s);s=inline(eq); % Converts the i/p string into symbolic functiony0=input(Enter y: );x0=input(Enter x: );xu=input(Enter unknown x: );h=input(Enter step size: );% Calculationn=(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 answerdisp(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 ORDEREnter the diff. eqn in x and y: 0*x+yEnter y: 2Enter x: 0Enter unknown x: 0.2Enter step size: 0.1The 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: 34Statement: Write down the Matlab Program for Milne’s correct prediction method .Solution:Input:clc; clear all; % Clears the workspacedisp(MILNE PREDICTION);format long% Take the input from usereq=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: );%calculationn=(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;enddisp(The value of the diff eqn at unkown x is: ); disp(yans);Output:MILNE PREDICTIONEnter the 1st diff. eqn in x, y: x-y+1Enter y: [0;0.1951;0.3812;0.5591]Enter x: [1;1.1;1.2;1.3]Enter unknown x: 1.5Enter 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: 35Statement: Write down the Matlab Program for Runge-Kutta simultaneous method.Solution:Input:clc; clear all; % Clears the workspacedisp(RUNGE KUTTA METHOD 4TH ORDER FOR SIMLTANEOUS EQUATONS);format long% Takes the input from usereq=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 functions1=inline(eq1,x,y,z); % Converts the i/p string into symbolic functiony0=input(Enter y: );x0=input(Enter x: ); z0=input(Enter z: );xu=input(Enter unknown x: );h=input(Enter step size: );% Calculationn=(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 answerdisp(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 EQUATONSEnter the 1st diff. eqn in x, y, z: x+y*zEnter the 2nd diff. eqn in x, y, z: x^2-y^2Enter y: 1Enter x: 0Enter z: 0.5Enter unknown x: 1.2Enter step size: 1.2The value of the diff eqn at unknown x is: 1.352724056760832The 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 userg=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 functionm=size(x); % Calculate the size of matrix x% Main calculationn=(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;endfprintf(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^2Enter 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.5Enter step size: 0.1The value at given x is : 0.740490 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 36Statement: Write down the Matlab Program for Parabolic method.Solution:Input :% Program for Parabollic Equation (Schmidt Method)clear all;clc;a=1; b=1;% inputxi=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; endendti=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; endends=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 matrixu(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 themfor i=3:co+1 u(2,i)=f(u(1,i));endfor 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); endend% display output Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• disp(u);Output:Enter initial value of x: 0Enter final value of x: 1Enter step size for x: 0.2Enter initial value of t: 0Enter final value of t: 0.006Enter step size for t: 0.002For all values of x at t=0, u(x)=sin(pi*x)Enter value of C: 1Enter constant value of u for x=xi: 0Enter constant value of u for x=xf: 0 0 0 0.2000 0.4000 0.6000 0.8000 1.0000 0 0 0.5878 0.9511 0.9511 0.5878 0 0.0020 0 0.5766 0.9329 0.9329 0.5766 0 0.0040 0 0.5655 0.9151 0.9151 0.5655 0 0.0060 0 0.5547 0.8976 0.8976 0.5547 0 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 37Statement: Write down the Matlab Program for Crank-Nicholeson method.Solution:Input : % Crank Nicolesonclear all;clc;a=1; b=1; c=1;% inputxi=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 matrixu(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));endui=u;k=1;% find central values of matrixwhile 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; endend Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• disp(u);Output:Enter initial value of x: 0Enter final value of x: 3Enter step size for x: 1Enter initial value of t: 0Enter final value of t: .3Enter step size for t: .1For all values of x at t=0, u(x)=x^2Enter value of C: 1Enter constant value of u for x=xi: 0Enter constant value of u for x=xf: 0 0 0 1.0000 2.0000 3.0000 0 0 1.0000 4.0000 0 0.1000 0 1.1333 0.5333 0 0.2000 0 0.2178 0.3378 0 0.3000 0 0.1046 0.0806 0 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 38Statement: Write down the Matlab Program for Hyperbolic method.Solution:Input :% Program to solve Hyperbolic Partial Differential Equationclear all;clc;a=1; b=1;% inputxi=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 matrixu(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 matrixfor i=3:co+1 u(2,i)=f(u(1,i));endfor i=3:co+1 u(3,i)=(u(2,i-1)+u(2,i+1))/2;endfor 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); endend% display outputdisp(u); Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Output:Enter initial value of x: 0Enter final value of x: 4Enter step size for x: 1Enter initial value of t: 0Enter final value of t: 2.5Enter step size for t: 0.5For all values of x at t=0, u(x)=(x^2)*(2-x)Enter value of C: 2Enter constant value of u for x=xi: 0Enter constant value of u for x=xf: 0 0 0 1.0000 2.0000 3.0000 4.0000 0 0 1.0000 0 -9.0000 0 0.5000 0 0 -4.0000 0 0 1.0000 0 -5.0000 0 5.0000 0 1.5000 0 0 4.0000 0 0 2.0000 0 9.0000 0 -1.0000 0 2.5000 0 0 4.0000 0 0 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Assignment No: 39Statement: Write down the Matlab Program for Elliptical method.Solution:Input :clear all;clc;% take user inputu=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 matrixan(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 endendso(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; endendansoan1=an;clear an;% solve the matrixt=GaussSoln(an1,so,n^2);k=1;% interpret the answersfor i=1:n for j=1:n t1(i,j)=t(k); k=k+1; endendt1hold off;% plot the answersfor 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; endendaxis 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 conditionsif 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 endend% answerSoln=an;Output:Temperature of upper surface: 100Temperature of left surface: 100Temperature of right surface: 0Temperature of lower surface: 0No. of elements in a row: 3an = 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 0t1 = 75.0000 50.0000 50.0000 25.0000 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Third Year Mechanical EngineeringComputer 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_stnew= td-(ft(td)/dft(td))epsilon_a= abs((tnew-td)/tnew)*100td=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)*100epsilon_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 A disp head while ea>=estemp=tt=f(t)ea=abs((t-temp)/t)*100et=abs((tr-t)/tr)*100 disp table END Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• FLOWCHART: Gauss-Naïve Elimination method Start Input matrices A&B [m,n]=size [A] NO Print “Matrix If m~=n must be square!” YES For k=1:n-1 A 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
• 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 A 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 D for i=n:-1:1 Third Year Mechanical Engineering C Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• C 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 EngineeringComputer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• FLOWCHART: Gauss-Seidel without Relaxation method Start Input matrices A&B [m,n]=size(a) NO Print “Matrix Must if (m~=n) Be Square!” YES For i= m:n d(i)=b(i)/a(i,i) d=d’ c=a For i=1:n B 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 A
• A c(i,i)=0 x(i)=0B 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 Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© End MITCOE Mechanical Engineering
• FLOWCHART: Gauss-Seidel with Relaxation method START Input matrix A(a) And B(b) [m,n]=size (a) YES Print “Matrix If m~=n must be square!” NO For k = 1:n-1 E [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 M Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• M 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 & dInput weighing factor (lambda) es=0.05 ea(i)=100 set head Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© N MITCOE Mechanical Engineering
• N Display head While ea(i)>=es For i=1:nY=x(i)X(i)=d(i)-c(I,:)*x(:,1)X(i)=lambda*x(i)+(1_lambda)*yea(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=0xy(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 A Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• A 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< = nsr(1,j)=(y(1,j)-a0-beta*x(1,j))^2 st(1,j)=(y(1,j)-ym)^2 j = j +1 B Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Bxp=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 A Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• A 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=0y(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 A Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• A Print x & ybeta=(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< = nsr(1,j)=(y(1,j)-a0-beta*x(1,j))^2 st(1,j)=(y(1,j)-ym)^2 j = j +1 B Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• B SR=sum(sr) ST=sum(st) r2=(ST-SR)/ST Print Function Of best fitted curvexp = 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
• STARTEnter values ofx & 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 A Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• A 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< = nsr(1,j)=(y(1,j)-a0-beta*x(1,j))^2 st(1,j)=(y(1,j)-ym)^2 j = j +1 B Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Bxp = 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 B If i=~j End p=p*(u-x(j))/(x(i)-x(j)) A Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• AB 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 A Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• A 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 B Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• BPrint 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 A Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• AB 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 C Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• C 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 i≠j 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
• A B 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
• A 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 BC 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) A Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• AFor j = 1 to (n-p)dy = y(j+1) – y(j) Display dy y = dy d(j+1)=y(1,p) f=0For k = 1 to (n-1) fr=d(k+1)/kf=f+((-1)^(k-1))*fr B Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Bdx=(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
• STARTEnter values of x & y in matrix form. & Enter value of X n = size of matrix x p=1h = 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) A Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• AFor j = 1 to (n-p)dy = y(j+1) – y(j) Display dy y = dy*(-1) d(j+1)=y(1,p) f=0For k = 1 to (n-1) fr=d(k+1)/k f=f+fr B Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Bdx=(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: Simpson’s 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 M Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• NO M t=2*y(a+i*h) YESt= 4*y(a+i*h) s=s+tA=h*(y(a)+y(b)+s)/3 print A END Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• FLOWCHART: Simpson’s 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 M t=2*y(a+i*h) YESt= 4*y(a+i*h) s=s+tA=h*(y(a)+y(b)+s)/3 print A END Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• FLOWCHART: Simpson’s 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 M t=2*y(a+i*h) YESt= 4*y(a+i*h) s=s+tA=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
• B A 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/4B Print A END Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• FLOWCHART: Double Integration by Simpson’s 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
• B A A for j=0:2:ny-1tr1=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 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:Heun’s 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| B Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• B x0=x1A 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); B Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• B x(m)=x(m)+h y3=ya1A 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 A 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
• BA Displaying yanswer END Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• FLOWCHART: Milne’s 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) A if y4pr≠p4cr B Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• B y4pr=y4cr y4=y4crA 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 A 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)) B Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• B c=(c1+2*c2+2*c3+c4)/6 d=(d1+2*d2+2*d3+d4)/6 yans=y0+c zans=z0+d y0=yansA 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); B Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• Third Year Mechanical EngineeringComputer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• B x(m)=x(m)+h y3=ya1A 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 A B C Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• A C B 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+1u(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 EngineeringComputer 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 Third Year Mechanical Engineering Computer Oriented Numerical Methods A 2011-12© MITCOE Mechanical Engineering
• A 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))/2A For i=4:ro+2 For j=3:co+1B Third Year Mechanical Engineering Computer Oriented Numerical Methods C 2011-12© MITCOE Mechanical Engineering
• C u(i,j)=u(i-1,j-1)+u(i-1,j+1)-u(i-2,j) BA 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 A For i=1:n^2 B For j=i:n^2 YES an(i,j)=4 IF i==j NO M IF YES mod(i,n)==1 (i,j)=-1 && j==i+1 M NO Third Year Mechanical Engineering Computer Oriented Numerical Methods C 2011-12© MITCOE Mechanical Engineering
• C YES IF j==i-n an(i,j)=-1 && j>0 NO M IF j==i+n YES an(i,j)=-1 && j<=n^2 NO M mod(i,n)==0 YES an(i,j)=-1 && j==i-1 NO M IF NO mod(i,n)>1 B && j==i+1 || j==i-1 ) YES an(i,j)=-1 M BA so(n)=0 Third Year Mechanical Engineering D Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• DE For i=1:n^2 YES If i==1 so(i)=u+l NO N YES If i>1 so(i)=u && i<n NO N If YES so(i)=u+r i==n NO N If mod(i,n)==1 YES so(i)=l && i>n && i<=n^2-n NO N mod(i,n)>1 && YES mod(i,n)<n && so(i)=0 i>n && i<=n^2-n NO N Third Year Mechanical Engineering Computer Oriented Numerical Methods F 2011-12© MITCOE Mechanical Engineering
• F YES If mod(i,n)==0 && i>n && so(i)=r i<=n^2-n NO N YES If i==n^2- so(i)=l+b n+1 NO N If i>n^2-n+1 YES so(i)=b && i<n^2 NO N NO If i==n^2 E YES so(i)=b+r NE Display an & so G Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© MITCOE Mechanical Engineering
• G 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:nscatter(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]) Third Year Mechanical Engineering Computer Oriented Numerical Methods 2011-12© End MITCOE Mechanical Engineering