[go: up one dir, main page]

0% found this document useful (0 votes)
37 views8 pages

(X, Y) Meshgrid (1:0.5:10,1:20) Z Sin (X) + Cos (Y) Surf (X, Y, Z)

The document contains code for plotting various 2D and 3D graphs using functions like meshgrid, surf, cylinder. It also contains implementations of root finding algorithms - bisection method, regula falsi method and Newton Raphson method to find the roots of nonlinear equations. Various examples of equations are solved using these three methods and the steps are clearly explained in the code sections.

Uploaded by

Dionis Daguman
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)
37 views8 pages

(X, Y) Meshgrid (1:0.5:10,1:20) Z Sin (X) + Cos (Y) Surf (X, Y, Z)

The document contains code for plotting various 2D and 3D graphs using functions like meshgrid, surf, cylinder. It also contains implementations of root finding algorithms - bisection method, regula falsi method and Newton Raphson method to find the roots of nonlinear equations. Various examples of equations are solved using these three methods and the steps are clearly explained in the code sections.

Uploaded by

Dionis Daguman
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/ 8

x=-1:.1:1;y=0:.1:1.

5;
[X,Y]=meshgrid(x,y);
2

F=X.^2+Y.^2;
surf(X,Y,F);
xlabel('x');
ylabel('y');

[X,Y] = meshgrid(1:0.5:10,1:20);
Z = sin(X) + cos(Y);
surf(X,Y,Z)

t = 0:pi/10:2*pi;
figure
[X,Y,Z] = cylinder(sin(t)+cos(t));
surf(X,Y,Z)
axis square

x=-1:.1:1;y=0:.1:1.5;
[X,Y]=meshgrid(x,y);
2

F=(cos(X)+sin(Y));
surf(X,Y,F);
xlabel('x');
ylabel('y');

x=-1:.1:1;y=0:.1:1.5;
[X,Y]=meshgrid(x,y);
2

F=Y.^2+5*cos(pi*X);
surf(X,Y,F);
xlabel('x');
ylabel('y');

x=4:4.2:5.2;
y=log(x);
trapz(x,y)

clear all
clc
syms x y
f=[x.^2-y.^2 2*x*y];
disp('Along the curve y=x.^2')
a=subs(f,y,x.^2);
b=diff(x.^2,x);
c=b*a(2);
d=int(a(1),x,0,1);
e=int(c,x,0,1);
u=d+e
disp('Along the curve y=sqrt(x)')
p=subs(f,y,sqrt(x));
q=diff(sqrt(x),x);
r=q*p(2);
s=int(p(1),x,1,0);
t=int(r,x,1,0);
v=s+t
I=u+v
x=-2:0.5:2;
y2=sqrt(x);
y1=x.^2;
plot(x,y1,'r', x,y2,'g');
grid on

f=@(x,y) (3*x^2+1);
[x,y]=ode23(f,[1:0.5:2],2)
[x,y]=ode45(f,[1:0.5:2],2)

f=@(x,y) (x+y+x*y);
[x,y]=ode23(f,[0:0.025:1],1)
[x,y]=ode45(f,[0:0.025:1],1)
f=@(x,y) (x.^2+y.^2);
[x,y]=ode23(f,[0:0.1:0],1)
[x,y]=ode45(f,[0:0.1:0],1)
f=@(x,y) (2*exp(x)-3*y);
[x,y]=ode23(f,[0:0.25:0],0)
[x,y]=ode45(f,[0:0.25:0],0)
9–a

%Bisection Method%
f=@(x) x^3-x-1;
display('Equation is x^3-x-1= 0')
i=1;
while(i)
xl=input('Enter lower value:');
xu=input('Enter upper value: ');
e=input('Enter accuracy: ');
if f(xl)*f(xu)<0
i=0;
else
warning('Enter proper range');
end
end
if f(xl)<0
xn=xl;
xp=xu;
else
xn=xu;
xp=xl;
end
while (abs(xn-xp)>e)
xm=(xn+xp)/2;
if f(xm)<0
xn=xm;
else
xp=xm;
end
end
Root=xm

%Regula Falsi%
f=@(x) x^3-x-1;
display('Equation is x^3-x-1=0')
i=1;
while(i)
xl=input('Enter lower value:');
xu=input('Enter upper value: ');
e=input('Enter accuracy: ');
if f(xl)*f(xu)<0
i=0;
else
warning('Enter proper range');
end
end
if f(xl)<0
xn=xl;
xp=xu;
else
xn=xu;
xp=xl;
end
xm=xl;
while (abs(f(xm))>e)
xm=(xn*f(xp)-xp*f(xn))/(f(xp)-f(xn));
if f(xm)<0
xn=xm;
else
xp=xm;
end
end
Root=xm

%Newton Raphson Method%

syms x
f= x^3-x-1;
fdash=diff(f);
disp('The equation is: '),disp(f);
disp('The derivative of equation is: '),disp(fdash);
y=inline(f);
dy=inline(fdash);
x0=input('Enter approximate root: ');
e=input('Enter the accuracy: ');
while abs(feval(y,x0))>e
h=-feval(y,x0)/feval(dy,x0);
x0=x0+h;
end
root=x0
9–b

%Bisection Method%
f=@(x) 4*sin(x)-exp(x);
display(‘Equation is 4*sin(x)-exp(x)=0’)
i=1;
while(i)
xl=input('Enter lower value:');
xu=input('Enter upper value: ');
e=input('Enter accuracy: ');
if f(xl)*f(xu)<0
i=0;
else
warning('Enter proper range');
end
end
if f(xl)<0
xn=xl;
xp=xu;
else
xn=xu;
xp=xl;
end
while (abs(xn-xp)>e)
xm=(xn+xp)/2;
if f(xm)<0
xn=xm;
else
xp=xm;
end
end
Root=xm

%Regula Falsi%
f=@(x) 4*sin(x)-exp(x);
display('Equation is 4*sin(x)-exp(x)=0’)
i=1;
while(i)
xl=input('Enter lower value:');
xu=input('Enter upper value: ');
e=input('Enter accuracy: ');
if f(xl)*f(xu)<0
i=0;
else
warning('Enter proper range');
end
end
if f(xl)<0
xn=xl;
xp=xu;
else
xn=xu;
xp=xl;
end
xm=xl;
while (abs(f(xm))>e)
xm=(xn*f(xp)-xp*f(xn))/(f(xp)-f(xn));
if f(xm)<0
xn=xm;
else
xp=xm;
end
end
Root=xm

%Newton Raphson Method%

syms x
f=4*sin(x)-exp(x);
fdash=diff(f);
disp('The equation is: '),disp(f);
disp('The derivative of equation is: '),disp(fdash);
y=inline(f);
dy=inline(fdash);
x0=input('Enter approximate root: ');
e=input('Enter the accuracy: ');
while abs(feval(y,x0))>e
h=-feval(y,x0)/feval(dy,x0);
x0=x0+h;
end
root=x0
9–c

%Bisection Method%
f=@(x) 2*x-cos(x)-3;
display('Equation is 2*x-cos(x)-3=0’)
i=1;
while(i)
xl=input('Enter lower value:');
xu=input('Enter upper value: ');
e=input('Enter accuracy: ');
if f(xl)*f(xu)<0
i=0;
else
warning('Enter proper range');
end
end
if f(xl)<0
xn=xl;
xp=xu;
else
xn=xu;
xp=xl;
end
while (abs(xn-xp)>e)
xm=(xn+xp)/2;
if f(xm)<0
xn=xm;
else
xp=xm;
end
end
Root=xm

%Regula Falsi%
f=@(x)2*x-cos(x)-3;
display('Equation is 2*x-cos(x)-3=0')
i=1;
while(i)
xl=input('Enter lower value:');
xu=input('Enter upper value: ');
e=input('Enter accuracy: ');
if f(xl)*f(xu)<0
i=0;
else
warning('Enter proper range');
end
end
if f(xl)<0
xn=xl;
xp=xu;
else
xn=xu;
xp=xl;
end
xm=xl;
while (abs(f(xm))>e)
xm=(xn*f(xp)-xp*f(xn))/(f(xp)-f(xn));
if f(xm)<0
xn=xm;
else
xp=xm;
end
end
Root=xm

%Newton Raphson Method%

syms x
f=2*x-cos(x)-3;
fdash=diff(f);
disp('The equation is: '),disp(f);
disp('The derivative of equation is: '),disp(fdash);
y=inline(f);
dy=inline(fdash);
x0=input('Enter approximate root: ');
e=input('Enter the accuracy: ');
while abs(feval(y,x0))>e
h=-feval(y,x0)/feval(dy,x0);
x0=x0+h;
end
root=x0

You might also like