0% found this document useful (0 votes)
50 views

Numerical Assignment

The document describes a numerical methods project conducted by 4 students on Heun's method and Runge-Kutta 4th order method. It includes the names and student IDs of the group members, the topic of numerical methods, and date submitted. Code examples are provided to demonstrate applying Heun's method and Runge-Kutta 4th order method to solve differential equations numerically. The output of running the code on sample problems is also shown.

Uploaded by

Cer No Rus
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Numerical Assignment

The document describes a numerical methods project conducted by 4 students on Heun's method and Runge-Kutta 4th order method. It includes the names and student IDs of the group members, the topic of numerical methods, and date submitted. Code examples are provided to demonstrate applying Heun's method and Runge-Kutta 4th order method to solve differential equations numerically. The output of running the code on sample problems is also shown.

Uploaded by

Cer No Rus
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

NUMERICAL METHOD FOR ENGINEERING

EKC 245
AHLI KUMPULAN

NAMA
EKMAL HARRIS B ABDUL MALEK

NO.MATRI
K
115836

MOHD KHAIRI B OTHMAN

117893

HASIF B MIOR HAKIM

115841

MOHAMAD AZZAM B CHE RAHIM

117890

TAJUK SOALAN
:
NAMA PENSYARAH
:
TARIKH HANTAR PROJEK
:

NUMERICAL METHOD
DR TYE CHIN THIAN
26 MEI 2014

1)Ekmal Harris B Abdul Malek(Ketua)


2)Mohd Khairi B Othman
3)Hasif B Mior Hakim
4)Mohamad Azzam B Che Rahim
%(Qa) HEUN'S METHOD
syms x y dy
dy=input('Please input the function:');
h=input('Please input the step size:');
x0=input('Please input the x0:');
y0=input('Please input the y0:');
xn=input('Please input the xn:');
n=(xn-x0)/h;
y_pred(1)=y0 + subs(dy,{x,y},{x0,y0})*h;
y_cor(1)=y0 + (subs(dy,{x,y},{x0,y0})+ subs(dy,{x,y},{x0+h,y_pred(1)}))/2*h;
y_cor(1)=y0 + (subs(dy,{x,y},{x0,y0})+ subs(dy,{x,y},{x0+h,y_cor(1)}))/2*h;
for k=1:n
xa(k)=x0+k*h;
end
for k=1:n-1
y_pred(k+1)=y_cor(k) + subs(dy,{x,y},{xa(k),y_cor(k)})*h;
y_cor(k+1) =y_cor(k) + (subs(dy,{x,y},{xa(k),y_cor(k)})+ subs(dy,{x,y},{xa(k)
+h,y_pred(k+1)}))/2*h;
y_cor(k+1) =y_cor(k) + (subs(dy,{x,y},{xa(k),y_cor(k)})+ subs(dy,{x,y},{xa(k)
+h,y_cor(k+1)}))/2*h;
end
y_cor(k+1)
plot(y_cor)

example 25.14
Please input the function:10*exp(-(x-2)^2/(2*0.075^2))-0.6*y
Please input the step size:0.1
Please input the x0:0
Please input the y0:0.5
Please input the xn:4
ans =
0.6125

1.8
1.6
1.4
1.2
1
0.8
0.6
0.4
0.2
0

10

15

20

25

30

35

40

Runge-kutta 4th order


%By Graft Cat Eisin (Fb Name)
meow=input('Ur equation in x and y only yaa dy/dx
disp(' the eq is in form of general dy/dx= meow')
cute_cat =inline(meow,'x','y');
h=input('Step Size = ');
a=input('y(x) where x= what value you want? ');
l=1.5*a;
c=input('initial x0= ');
x = c:h:l;
y = zeros(1,length(x));
y(1) = input('Initial condition y(x0) = ');

=','s');
% step size
% User input value of x
% Higher Interval of graph
% Calculates upto y(a)
%initial condition

for i=1:(length(x)-1)
% calculation loop
k_1 = cute_cat(x(i),y(i));
k_2 = cute_cat(x(i)+0.5*h,y(i)+0.5*h*k_1);
k_3 = cute_cat((x(i)+0.5*h),(y(i)+0.5*h*k_2));
k_4 = cute_cat((x(i)+h),(y(i)+k_3*h));
y(i+1) = y(i) + (1/6)*(k_1+2*k_2+2*k_3+k_4)*h; % main equation
end
y
disp('Ur value of y would be y = ')
y((abs(c)+a)/h+1)
plot(x,y,a,y((abs(c)+a)/h+1),'*')

%Answer for x

Example 25.14
Ur equation in x and y only yaa dy/dx =10*exp(-(x-2)^2/(2*0.075^2))-0.6*y

meow =
10*exp(-(x-2)^2/(2*0.075^2))-0.6*y
the eq is in form of general dy/dx= meow
Step Size = 0.05
Maximum x = 4
initial x0= 0
Initial condition y(x0) = 0.5
y=
Columns 1 through 9
0.5000
0.3933

0.4852

0.4709

0.4570

0.4435

0.4304

0.4176

0.4053

0.3488

0.3385

0.3285

0.3188

0.3094

0.2663

0.2584

0.2508

0.2434

0.2362

0.2033

0.1973

0.1914

0.1858

0.1804

0.6173

1.0578

1.4847

1.7390

1.8140

1.5988

1.5515

1.5057

1.4612

1.4180

Columns 10 through 18
0.3817
0.3002

0.3704

0.3595

Columns 19 through 27
0.2914
0.2292

0.2828

0.2744

Columns 28 through 36
0.2224
0.1758

0.2159

0.2095

Columns 37 through 45
0.1769
1.7953

0.2069

0.3278

Columns 46 through 54
1.7485
1.3761

1.6976

1.6475

Columns 55 through 63

1.3354
1.0505

1.2960

1.2577

1.2205

1.1844

1.1494

1.1154

1.0825

0.9317

0.9042

0.8774

0.8515

0.8263

0.7112

0.6902

0.6698

0.6500

0.6308

0.5429

0.5269

0.5113

0.4962

0.4815

0.4145

0.4022

0.3903

0.3788

0.3676

0.3164

0.3070

0.2980

0.2892

0.2806

0.2415

0.2344

0.2275

0.2207

0.2142

Columns 64 through 72
1.0194
0.8019

0.9893

0.9601

Columns 73 through 81
0.7782
0.6122

0.7552

0.7329

Columns 82 through 90
0.5941
0.4673

0.5765

0.5595

Columns 91 through 99
0.4535
0.3567

0.4401

0.4271

Columns 100 through 108


0.3462
0.2723

0.3360

0.3260

Columns 109 through 117


0.2643
0.2079

0.2565

0.2489

Columns 118 through 121


0.2017

0.1958

0.1900

Ur value of y would be y =
ans =
0.6122

0.1844

2
1.8
1.6
1.4
1.2
1
0.8
0.6
0.4
0.2
0

Adaptive Runge Kutta:


syms x y
ydot=input('Please input the derivative function=');
h=input('Please input step size=');
xi=input('Please input initial x=');
yi=input('Please input initial y=');
xf=input('Please input final x=');
epsilon=input('Please input tolerance=');
n=input('Please input maximum number of change for h=');
v=n/h;
for i=1:v
x1=xi(i);
y0=yi(i);
k1full=subs(ydot,{x,y},[x1,y0]);
k2full=subs(ydot,{x,y},[x1+1/2*h,y0+1/2*k1full*h]);
k3full=subs(ydot,{x,y},[x1+1/2*h,y0+1/2*k2full*h]);
k4full=subs(ydot,{x,y},[x1+h,y0+k3full*h]);
y1=y0+1/6*h*(k1full+2*k2full+2*k3full+k4full);
for j=1:2
k1half=subs(ydot,{x,y},[x1,y0]);
k2half=subs(ydot,{x,y},[x1+1/4*h,y0+1/4*k1half*h]);
k3half=subs(ydot,{x,y},[x1+1/4*h,y0+1/4*k2half*h]);
k4half=subs(ydot,{x,y},[x1+1/2*h,y0+1/2*k3half*h]);
y0=y0+1/6*h*(k1half+2*k2half+2*k3half+k4half);
x1=x1+1/2*h;
end
delta_present=y0-y1;

y_scale=abs(yi(i))+abs(h*k1full);
delta_new=0.1*epsilon*y_scale;
if delta_new >= delta_present
h=h*abs(delta_new/delta_present)^0.2;
else
h=h*abs(delta_new/delta_present)^0.25;
end
if xi(i)+h>=xf
h=xf-xi(i);
end
k1=subs(ydot,{x,y},[xi(i),yi(i)]);
k2=subs(ydot,{x,y},[xi(i)+1/2*h,yi(i)+1/2*k1*h]);
k3=subs(ydot,{x,y},[xi(i)+1/2*h,yi(i)+1/2*k2*h]);
k4=subs(ydot,{x,y},[xi(i)+h,yi(i)+k3*h]);
yi(i+1)=yi(i)+1/6*h*(k1+2*k2+2*k3+k4);
xi(i+1)=xi(i)+h;
if xi(i+1)>=xf
break
else
continue
end

end
yi
plot(xi,yi,'xk',xi,yi)

Example 25.14
Please input the derivative function=10*exp(-(x-2)^2/(2*0.075^2))-0.6*y
Please input step size=0.05
Please input initial x=0
Please input initial y=0.5
Please input final x=4
Please input tolerance=1
Please input maximum number of change for h=10

2
1.8
1.6
1.4
1.2
1
0.8
0.6
0.4
0.2
0

0.5

1.5

2.5

3.5

You might also like