0% found this document useful (0 votes)
36 views9 pages

HRX

The document describes and provides code examples for numerical methods including the secant method, Newton-Raphson method, power method, and Runge-Kutta method. Code is provided to find the root of non-linear equations, compute eigenvalues and eigenvectors of a matrix, and solve ordinary differential equations.

Uploaded by

Hrutik Nimbalkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views9 pages

HRX

The document describes and provides code examples for numerical methods including the secant method, Newton-Raphson method, power method, and Runge-Kutta method. Code is provided to find the root of non-linear equations, compute eigenvalues and eigenvectors of a matrix, and solve ordinary differential equations.

Uploaded by

Hrutik Nimbalkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Computer Techniques in Chemical Engineering

Secant Method
clc
function c=secant(f, x0, x1, tol)
i=1;
n(1)=100;
if f(x0)*f(x1)<0
a(1)=x0;
b(1)=x1;
c(1)=(a(1)*f(b(1))-b(1)*f(a(1)))/(f(b(1))-f(a(1)));
printf('i.\t\ta\t\tc\t\tb\t\tf(c)\tGalat\n');
printf('%2d\t%11.6f\t%11.6f\t%11.6f\t%11.6f\n',i,a(i),c(i),b(i),f(c(i)));
while abs(n(i))>=tol,
a(i+1)=a(i);
b(i+1)=c(i);
c(i+1)=(a(i+1)*f(b(i+1))-b(i+1)*f(a(i+1)))/(f(b(i+1))-f(a(i+1)));
n(i+1)=abs((c(i+1)-c(i)));
r(i+1)=abs((c(i+1)-c(i))/(c(i+1)))*100;
printf('%2d\t%11.6f\t%11.6f\t%11.6f\t%11.6f\t%7.6f\n',i+1,a(i+1),c(i+1),b(i+1),f(c(i
+1)),n(i+1)),r(i+1);
i=i+1;
end
else
printf('');
end
endfunction
function y=myfunc(x)
y=x^3-3*x^2+2.5
endfunction
secant(myfunc,1,2,0.0001)
scf(1);
clf(1);
x=linspace(-2,10,25)
plot(x,myfunc(x),'o-o');
set(gca(),"grid",[11]);
a=gca();
axlocation="origin";
aylocation="origin";
xlabel("$x$","fontsize",4,"color","blue");
ylabel("$y=x^3-3*x^2+2.5$","fontsize",4,"color","blue");
title("runge function("+string(length(x))+")","color","red","fontsize","4")
legend("function evaluation");

T.K.I.E.T, Warnanagar Page 1


Computer Techniques in Chemical Engineering

i. a c b f(c) Galat

1 1.000000 1.250000 2.000000 -0.234375

2 1.000000 1.170213 1.250000 -0.005707 0.079787

3 1.000000 1.168292 1.170213 -0.000109 0.001921

4 1.000000 1.168255 1.168292 -0.000002 0.000037

T.K.I.E.T, Warnanagar Page 2


Computer Techniques in Chemical Engineering

T.K.I.E.T, Warnanagar Page 3


Computer Techniques in Chemical Engineering

Newton-Raphson Method
clc
clear
function [f, df]=fun7(x)
f=x^2-3
df=2*x
endfunction
xold=1;
maxit=2;
iter=1;
while(1)
[fx,dfx]=fun7(xold);
xnew=xold-fx/dfx;
if iter==maxit then
break
end
xold=xnew;
iter=iter+1;
end
root=round(xnew*10^3)/10^3;
disp(root,"root=")

T.K.I.E.T, Warnanagar Page 4


Computer Techniques in Chemical Engineering

T.K.I.E.T, Warnanagar Page 5


Computer Techniques in Chemical Engineering

Power method
clear
clc;
close();
A=[1,-3,2;4,4 -1;6,3,5];
u0=[1,0,0]';
v=A*u0;
a=max(u0);
while abs (max(v)-a)>0.005
a=max(v);
u0=v/max(v);
v=A*u0;
end
format('v',3);
disp(max(v),'eigen values:')
format('v',3);
disp(u0,'eigen vector:')

T.K.I.E.T, Warnanagar Page 6


Computer Techniques in Chemical Engineering

T.K.I.E.T, Warnanagar Page 7


Computer Techniques in Chemical Engineering

Runge kutta method


1. clc ;
2. clear ;
3. close ;
4. deff ('y=f(x,y)','y=x-y')
5. y=1;x=1;h=0.1;
6. k1=h*f(x,y) ;
7. k2=h*f(x+h/2,y= k1/2);
8. k3=h*f(x+h/2,y+k2/2);
9. k4=h*f(x+h,y+k3);
10. disp(k4,'k4=',k3,'k3=','k2=',k1,'k1=')
11. y1=y+(k1+2*k2/2*k3+k4)/6
12. printf('\ny(1.1)= %.8f\n\n',y1)

T.K.I.E.T, Warnanagar Page 8


Computer Techniques in Chemical Engineering

T.K.I.E.T, Warnanagar Page 9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy