Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
270 views35 pages

MATLAB Practical File (Codes) by Priyanshu Sinha

Submitted towards completion of the course NEOM by Priyanshu Sinha, Department of Electrical Engineering, DTU. MATLAB (Matrix Laboratory) is a numerical computing environment and programming language. It is widely used in industry and academia for data analysis, modeling, and simulation. Projects in MATLAB can range from simple data visualization and analysis to ccomplex simulations and modeling.

Uploaded by

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

MATLAB Practical File (Codes) by Priyanshu Sinha

Submitted towards completion of the course NEOM by Priyanshu Sinha, Department of Electrical Engineering, DTU. MATLAB (Matrix Laboratory) is a numerical computing environment and programming language. It is widely used in industry and academia for data analysis, modeling, and simulation. Projects in MATLAB can range from simple data visualization and analysis to ccomplex simulations and modeling.

Uploaded by

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

NEOM (MC261)

PRACTICAL FILE

PRIYANSHU SINHA
2K21/EE/227

EE-D BATCH (P2)

Submitted to : DR. DHEERAJ JOSHI


CONTENTS
AIM: TO FIND THE ROOT OF THE EQUATION USING NEWTON-RAPHSON METHOD. 3

Theory: 3

Code: 4

Output: 4

Flowchart: 5

AIM: TO FIND THE ROOT OF THE EQUATION USING REGULA-FALSI METHOD. 6

Theory: 6

Code: 7

Output: 7

Flowchart: 8

AIM: TO FIND THE ROOT OF THE EQUATION USING BISECTION METHOD. 9

Theory: 9

Code: 10

Output: 10

Flowchart: 11

AIM: TO SOLVE THE DIFFERENTIAL EQUATION USING RK-3RD ORDER. 12

Theory: 12

Code: 13

Output: 14

Flowchart: 14

AIM: TO SOLVE THE DIFFERENTIAL EQUATION USING RK-3RD ORDER. 15

Theory: 15

Code: 16

Output: 16

Flowchart: 17

AIM: TO SOLVE THE DIFFERENTIAL EQUATION USING RK-4TH ORDER. 18

1
Theory: 18

Code: 19

Output: 20

Flowchart: 20

AIM: TO FIND THE LOCAL MINIMA OF A FUNCTION USING FIBONACCI SEARCH METHOD. 21

Theory: 21

Code: 22

Output: 22

Flowchart: 23

AIM: TO FIND THE LOCAL MAXIMA OF A FUNCTION USING GOLDEN SECTION METHOD. 24

Theory: 24

Code: 25

Output: 25

Flowchart: 26

AIM: TO FIND THE MINIMUM OF A FUNCTION USING UNIDIRECTIONAL SEARCH METHOD.


27

Theory: 27

Code: 28

Output: 29

AIM: TO FIND THE MINIMA OF A FUNCTION USING PATTERN SEARCH METHOD 30

Theory: 30

Code: 31

Output: 32

AIM: TO FIND THE MINIMA OF A FUNCTION USING EVOLUTIONARY SEARCH METHOD 33

Theory: 33

Code: 34

Output: 34

2
AIM: TO FIND THE ROOT OF THE EQUATION USING
NEWTON-RAPHSON METHOD.

Theory:

3
Code:
format long;
x = input('Starting guess = ');
func = @(x) (sin(x)+3*x-exp(x));
dfunc = @(x) (cos(x)+3-exp(x));
tolerance = 0.0001;
iterations = 0;
while (iterations<100) && (abs(func(x))>tolerance)
x = x-(func(x)/dfunc(x))
iterations = iterations + 1;
end
if iterations==100
disp('No root found')
else
disp(x)
end

Output:

4
Flowchart:

5
AIM: TO FIND THE ROOT OF THE EQUATION USING REGULA-FALSI
METHOD.

Theory:

6
Code:
f= @(x)(sin(x)+3*x-exp(x));

a = input('Enter First Point = ');


b = input('Enter Second Point = ');
tol=input('Enter tolerance = ');
n=0;
c=0;

while n<=100
n=n+1;
if f(c)==0||abs(f(c))<=tol
root=c;
break;
end
c=b-f(b)*(b-a)/(f(b)-f(a));
if f(a)*f(c)<0
b=c;
elseif f(c)*f(b)<0
a=c;
end
end
root=c

Output:

7
Flowchart:

8
AIM: TO FIND THE ROOT OF THE EQUATION USING BISECTION
METHOD.

Theory:

9
Code:
f= @(x)(sin(x)+3*x-exp(x));

a = input('Enter First Point = ');


b = input('Enter Second Point = ');
tol=input('Enter tolerance = ');
n=0;
c=0;

n=0;
while n<=N
c=(a+b)/2;
if f(c)==0||(b-a)/2<tol
root=c
break;
end
n=n+1;

if f(a)*f(c)<0
b=c;
elseif f(c)*f(b)<0
a=c;
end
n=n+1;
end

Output:

10
Flowchart:

11
AIM: TO SOLVE THE DIFFERENTIAL EQUATION USING RK-3RD ORDER.

Theory:

12
Code:
clear all
clc
f=@(x,y)4-2*x*y;
x0=input('\n Enter initial value of x i.e. x0: ');
y0=input('\n Enter initial value of y i.e. y0: ');
xn=input('\n Enter the final value of x: ');
h=input('\n Enter the step length h: ');
fprintf('\n x y ');
count=0;
while x0<=xn
fprintf('\n%4.3f %4.3f ',x0,y0);
k1=h*f(x0,y0);
x1=x0+h;
k2=h*f(x1,y0+k1);
y1=y0+(k1+k2)/2;
p(count+1)=[x1];
q(count+1)=[y1];
count = count + 1;
x0=x1;
y0=y1;
end
plot(p,q)
title("f(x,y)=y-x^2+1")
xlabel("x")
ylabel("y")
[p]
[q]
grid on

13
Output:

Flowchart:

14
AIM: TO SOLVE THE DIFFERENTIAL EQUATION USING RK-3RD ORDER.

Theory:

15
Code:
clc;
f=@(x,y)4-2*x*y;
x0=input('Enter initial value of x0: ');
y0=input('Enter initial value of y0: ');
h=input('Enter the step length h: ');
n=input('Enter the times of interval(n) :');
terminal = x0 + h*n;
while x0<terminal
k1=h*f(x0,y0);
x1=x0+(h/2);
k2=h*f(x1,y0+(k1/2));
x1=x0+h;
c=h*f(x1,y0+k1);
k3=h*f(x1,y0+c);
m=(k1+4*k2+k3)/6;
fprintf('k=%4f \t',m);
y1=y0+m/6;
x0=x1;
y0=y1;
fprintf('At %4f the value of function is %4f \n',x0,y0);
end
final=y0;
fprintf('The final value is y=%4f at x=%4f', final, xn);

Output:

16
Flowchart:

17
AIM: TO SOLVE THE DIFFERENTIAL EQUATION USING RK-4TH ORDER.

Theory:

18
Code:
f = @(x,y) (4-2*x*y);
a = input('Enter left end point, a: ');
b = input('Enter right end point, b: ');
n = input('Enter no. of subintervals, n: ');
alpha = input('Enter the initial condition, alpha: ');
count=0;
h = (b-a)/n;
t = a;
w = alpha;
fprintf(' t w\n');
fprintf('%5.3f %11.7f\n', t, w);

for i = 1:n
k1 = h*f(t,w);
k2 = h*f(t+h/2.0, w+k1/2.0);
k3 = h*f(t+h/2.0, w+k2/2.0);
k4 = h*f(t+h,w+k3);
w = w+(k1+2.0*(k2+k3)+k4)/6.0;
t = a+i*h;
p(count+1)=[t];
q(count+1)=[w];
count = count +1;
fprintf('%5.3f %11.7f\n', t, w);
end
plot(p,q)
title("f(x,y)=4-2*x*y")
xlabel("x")
ylabel("y")
[p]
[q]
grid on;

19
Output:

Flowchart:

20
AIM: TO FIND THE LOCAL MINIMA OF A FUNCTION USING FIBONACCI
SEARCH METHOD.

Theory:

21
Code:
clc;
f = @(x) x*x - 2*x + 1;
a = 0.1; b = 1.95;
L = b-a; n = 3;
fib(1) = 1;
fib(2)= 1;
fprintf(" %d %d", fib(1), fib(2));
for i=3:n+1
fib(i) = fib(i-1) + fib(i-2);
fprintf(" %d", fib(i));
end
count = 2;
syms Lk;
L = b-a;
while count <= n
Lk = (fib(n-count+1)*L)/fib(n+1);
left_bound = f(a+Lk);
right_bound = f(b-Lk);
fprintf("\n a=%d b=%d x1=%d x2=%d f(x1)=%d f(x2)=%d\n", a,
b, a+Lk, b-Lk, left_bound, right_bound);
if left_bound > right_bound
a = a+Lk;
elseif left_bound < right_bound
b = b-Lk;
else
a = a+Lk;
b = b-Lk;
end
count = count + 1;
end
fprintf("\n The final point of uncertainity is between the
points %d and %d\n", a, b);
fprintf("\n f(a)=%d f(b)=%d\n", f(a), f(b));
fprintf("\n n = %d\n", n);

22
Output:

Flowchart:

23
AIM: TO FIND THE LOCAL MAXIMA OF A FUNCTION USING GOLDEN
SECTION METHOD.

Theory:

24
Code:
clc;
f = @(x) x*x + 2*x - 1;
a = 0.1; b = 1.95;
L = b-a; n = 7;
syms Lk;
gamma = 1.618;
count = 1;
epsilon = 0.05;
L0 = 1.85;
while count <= n
L = b-a;
if L < epsilon
break
end
fprintf("a= %d b=%d ", a, b);
L_star = (1/(gamma).^(1+count))*L0;
left_bound = f(a+L_star);
right_bound = f(b-L_star);
fprintf("x1= %d x2= %d f(x1)= %d f(x2)= %d\n", a+L_star,
b-L_star, left_bound, right_bound);
if left_bound > right_bound
b = b-L_star;
elseif left_bound < right_bound
a = a+L_star;
else
a = a+L_star;
b = b-L_star;
end
count = count + 1;
end
z=count-1;
fprintf("The final interval of uncertainity is between the
points %d and %d\n", a, b);
fprintf("f(a) = %d f(b) = %d\n", f(a), f(b));
fprintf("n=%d\n", z);

25
Output:

Flowchart:

26
AIM: TO FIND THE MINIMUM OF A FUNCTION USING
UNIDIRECTIONAL SEARCH METHOD.

Theory:

27
Code:
clc
f = @(x) (2*x-8).^2+(5*x-9).^2;
% function in one variable using initial guess as [2 1] and direction as [2 5]

x1(1)=0.5;
delta=1;
k=1;
if f(x1(1)-delta)>=f(x1(1)) && f(x1(1))>=f(x1(1)+delta)
delta =1;
else
delta =-1;
end

for i=1:10
x1(k+1)=x1(k)+2^(k-1)*delta;
if f(x1(k+1))<f(x1(k))
k=k+1;
else
break;
end
end
x1(k+1)=x1(k)+2^(k-1)*delta;
result = [x1(k-1) x1(k+1) k]

a=x1(k-1); b=x1(k+1); l=b-a; e=0.0001;


fn=@(w) (2*(3*w+0.5)-8).^2+(5*(3*w+0.5)-9).^2;
k1=1; aw=0; bw=1; lw=1;
fprintf('aw bw w1 w2 fw1 fw2 \n');
while (lw>e)
w1=aw + 0.618*lw;
w2=bw - 0.618*lw;
fw1=fn(w1);
fw2=fn(w2);
if w1>w2
if (fw1>fw2)
bw=w1;
else
aw=w2;
end
elseif w2>w1
if (fw1>fw2)
aw=w1;
else
bw=w2;
end
end

28
res(k1,:)=[aw bw w1 w2 fw1 fw2];
fprintf('%6f %6f %6f %6f %15f %15f \n',res(k1,:));
lw=bw-aw;
k1=k1+1;
end
display('value at which the single variable function is minimum');
display(3*aw+0.5);
display('value of x1 and x2');
display(2+2*(3*aw+0.5));
display(1+5*(3*aw+0.5));
ff=@(xx1,xx2) (xx1-10).^2+(xx2-10).^2;
ff(2+2*(3*aw+0.5),1+5*(3*aw+0.5))

Output:

29
AIM: TO FIND THE MINIMA OF A FUNCTION USING PATTERN SEARCH
METHOD

Theory:

30
Code:
clc;

syms x1 x2
fx = @(x1, x2) (x1-10).^2+(x2-10).^2;
fobj = @(x) fx(x(:,1),x(:,2));

d=0.8;
tol=1e-8;
x0 = [0 0];
x00=[0 0];
max=20;
i=0;
while gradient(fobj(x0))<tol
f0=fx(x0(1),x0(2));
fplus=fx(x0(1)+d,x0(2));
fminus=fx(x0(1)-d,x0(2));
if fplus<fminus && fplus<f0
x0(1)=x0(1)+d;
elseif fminus<fplus && fminus<f0
x0(1)=x0(1)-d;
else
x0(1)=x0(1);
end
f1=fx(x0(1),x0(2));
fplus1=fx(x0(1),x0(2)+d);
fminus1=fx(x0(1),x0(2)-d);
if fplus1<fminus1 && fplus1<f1
x0(2)=x0(2)+d;
elseif fminus1<fplus1 && fminus1<f1
x0(2)=x0(2)-d;
else
x0(2)=x0(2);
end
if x0==x00
d=d/2;
end

syms y
s=x0 - x00;
xnew=x0+y.*s;
k = diff(fobj(xnew),'y')==0;
y1= solve(k);
xn=x0 + y1*s;
x00=x0;
x0=xn;
i=i+1;

31
double(x0(1))
double(x0(2))
double(fobj(x0))
end

Output:

32
AIM: TO FIND THE MINIMA OF A FUNCTION USING EVOLUTIONARY
SEARCH METHOD

Theory:

33
Code:
clc;
f = @(x1,x2)(x1^2+x2-11).^2+(x1+x2^2-7).^2;
fobj=@(x) f(x(:,1),x(:,2));
x0=[0 0];
x1=[0 0];
x11=0;
x22=0;
d=0.1;

for i=1:100
p1= [x0(1)-d x0(2)-d];
p2= [x0(1)-d x0(2)+d];
p3= [x0(1)+d x0(2)-d];
p4= [x0(1)+d x0(2)+d];
result(i,:)=[fobj(p1) fobj(p2) fobj(p3) fobj(p4)];
if min(result(i,:))==fobj(p1)
x1= [x0(1)-d x0(2)-d];
elseif min(result(i,:))==fobj(p2)
x1= [x0(1)-d x0(2)+d];
elseif min(result(i,:))==fobj(p3)
x1= [x0(1)+d x0(2)-d];
else
x1= [x0(1)+d x0(2)+d];
end
if x1 == x0
d=d/2;
end
x0=x1;
end

display(x1);
fprintf('THE MINIMUM VALUE OF FUNCTION IS = %f',fobj(x1));

Output:

34

You might also like