Sei sulla pagina 1di 9

BSC208A

MATLAB Programs

1. Consider the ordinary differential equation


𝒅𝒚
= 𝒚 + 𝒕𝟐 − 𝟏, 𝒚(𝟏) = 𝟕.
𝒅𝒙
Obtain the solution 𝒚 at points 𝒕 ∈ [𝟏, 𝟕] using ‘ode45’. Verify the solution using ‘dsolve’. Hence
plot both exact and numerical solution in the same figure.

Program:

%% Solution using ode45 and dsolve


tspan = [1, 7];
y0 = 7; % initial condition i.e,y at t=1.
f = @(t,y) y+t^2-1;
[t, y] = ode45(f, tspan, y0);
plot(t,y,'*')
hold on
y = dsolve('Dy=y+t^2-1','y(1)=7','t' );
t = linspace(1,7,50);
y = eval(y);
plot(t,y,'r')

Output:

4500
solution using ode45
4000 solution using dsolve

3500

3000
solution y

2500

2000

1500

1000

500

0
1 2 3 4 5 6 7
t-axis

2. Consider :
𝒅𝟐 𝒚 𝟐
𝒅𝒚
− 𝒕 − 𝟐𝒕𝒚 = 𝟏, 𝒚(𝟎) = 𝟏, 𝒚′ (𝟎) = 𝟎.
𝒅𝒕𝟐 𝒅𝒕

Use ‘ode45’ to find the solution 𝒚 at points 𝒙 = 𝟎. 𝟏 to 1 in steps of 0.2;


Solution: First write second order ODE in terms of system of two first order ODE.

Put
𝑑𝑦
= 𝑧,
𝑑𝑡
Given ODE implies
𝑑𝑧
= 𝑡 2 𝑧 + 2𝑡𝑦 + 1.
𝑑𝑡
For simplicity, set 𝑦 = 𝑦1 and 𝑧 = 𝑦2.
With this above system of equations become
𝑑𝑦1
= 𝑦2,
𝑑𝑡
𝑑𝑦2
= 𝑡 2 𝑦2 + 2𝑡 𝑦1 + 1.
𝑑𝑡
Conditions 𝑦1(0) = 1, 𝑦2(0) = 0.

Program:

%% Solution using ode45


tspan = 0:0.2:1;
y0 = [1, 0];
f = @(t,y) [y(2); t^2*y(2)+2*t*y(1)+1];
[t, y] = ode45(f, tspan, y0);
n = length(tspan);
for i = 1:n
fprintf('solution y at t=%f is %f\n',t(i),y(i,1))
end
plot(t,y(:,1),'-*')

Output:
>> ODE2
solution y at t=0.000000 is 1.000000
solution y at t=0.200000 is 1.022702
solution y at t=0.400000 is 1.102595
solution y at t=0.600000 is 1.262645
solution y at t=0.800000 is 1.541062
solution y at t=1.000000 is 2.009335

2.4

2.2

1.8

1.6

1.4

1.2

1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
3. Write MATLAB function for the initial value problem using Euler’s method
𝒅𝒚
= 𝐜𝐨𝐬 𝒕 + 𝐬𝐢𝐧 𝒕 , 𝒚(𝟎) = 𝟏
𝒅𝒕
and determine the values of 𝒚 at 𝒕 = 𝟎. 𝟖 with 𝒉 = 𝟎. 𝟐.

Program:

function [] = myeuler(f,a,b,ya,h)
t = a:h:b;
n = length(t);
y = zeros(1,n);
y(1)=ya;
for i=1:n-1
y(i+1)=y(i)+h*f(t(i),y(i));
fprintf('Approximate value of y(%f)=%f \n', t(i+1), y(i+1))
end
plot(t,y,'-*')

output:
>> a=0; b=0.8; h=0.2; ya=1;
>> f = @(t,y)cos(t)+sin(t);
>> myeuler(f,a,b,ya,h)
Approximate value of y(0.200000)=1.200000
Approximate value of y(0.400000)=1.435747
Approximate value of y(0.600000)=1.697843
Approximate value of y(0.800000)=1.975839

1.9

1.8

1.7

1.6

1.5

1.4

1.3

1.2

1.1

1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
4. MATLAB function to perform Modified Euler method for the above question:

Program:

function [] = mymodeuler(f,a,b,ya,h)
t = a:h:b;
n = length(t);
maxit=11;
tre = 10^-6;
y = zeros(n,maxit);
y(1,:)=ya;
for i = 1:n-1
y(i+1,1)=y(i,end)+h*f(t(i),y(i,end));
for j=2:maxit
y(i+1,j)=y(i,end)+(h/2)*(f(t(i),y(i,end))+f(t(i+1),y(i+1,j-1)));
if abs(y(i+1,j)-y(i+1,j-1))<tre
y(i+1,j:end)=y(i+1,j);
break
end
end
fprintf('Approximate value of y(%f)=%f \n', t(i+1), y(i+1,j))
end
plot(t,y(:,end),'-*')

Output:
>> a=0; b=0.8; h=0.2; ya=1;
>> f = @(t,y)cos(t)+sin(t);
>> mymodeuler(f,a,b,ya,h)
Approximate value of y(0.200000)=1.217874
Approximate value of y(0.400000)=1.466795
Approximate value of y(0.600000)=1.736841
Approximate value of y(0.800000)=2.017245

2.4

2.2

1.8

1.6

1.4

1.2

1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
5. MATLAB function to perform Runge - Kutta method of 4th order to solve:

𝒅𝒚
= 𝟑𝒙𝟐 𝒆𝒙 − 𝒚, 𝒚(𝟎) = 𝟏,
𝒅𝒙
in the interval [𝟎, 𝟓] with step size 𝒉 = 𝟎. 𝟐.
Program:

function [] = RK4(f,a,b,ya,h)
t = a:h:b;
n = length(t);
y = zeros(1,n);
y(1)=ya;
for i = 1:n-1
k1 = h*f(t(i),y(i));
k2 = h*f(t(i)+h/2,y(i)+k1/2);
k3 = h*f(t(i)+h/2,y(i)+k2/2);
k4 = h*f(t(i)+h,y(i)+k3);
y(i+1)= y(i)+1/6*(k1+2*k2+2*k3+k4);
fprintf('Approximate value of y(%f)=%f \n', t(i+1), y(i+1))
end
plot(t,y,'.')
end

Output:
>> a=0; b=5; h=0.2; ya=1;
>> f = @(t,y) 3*t^2*exp(t)-y;
>> RK4(f,a,b,ya,h)
Approximate value of y(0.200000)=0.827620
Approximate value of y(0.400000)=0.749444
Approximate value of y(0.600000)=0.847925
Approximate value of y(0.800000)=1.247516
Approximate value of y(1.000000)=2.130928
Approximate value of y(1.200000)=3.760999
Approximate value of y(1.400000)=6.509960
Approximate value of y(1.600000)=10.898395
Approximate value of y(1.800000)=17.646904
Approximate value of y(2.000000)=27.744342
Approximate value of y(2.200000)=42.537660
Approximate value of y(2.400000)=63.849821
Approximate value of y(2.600000)=94.134124
Approximate value of y(2.800000)=136.675679
Approximate value of y(3.000000)=195.853798
Approximate value of y(3.200000)=277.482993
Approximate value of y(3.400000)=389.255211
Approximate value of y(3.600000)=541.312293
Approximate value of y(3.800000)=746.985683
Approximate value of y(4.000000)=1023.750673
Approximate value of y(4.200000)=1394.455472
Approximate value of y(4.400000)=1888.901959
Approximate value of y(4.600000)=2545.875895
Approximate value of y(4.800000)=3415.751028
Approximate value of y(5.000000)=4563.825140
5000

4500

4000

3500

3000

2500

2000

1500

1000

500

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

6. Write a MATLAB program to solve the heat equation by explicit method.


𝝏𝒖 𝟏 𝝏𝟐 𝒖
= , 𝒖(𝟎, 𝒕) = 𝟎, 𝒖(𝟑, 𝒕) = 𝟎, 𝟎 ≤ 𝒕 ≤ 𝟎. 𝟓
𝝏𝒕 𝟐𝟎 𝝏𝒙𝟐
𝒖(𝒙, 𝟎) = 𝟐(𝟏 + 𝐬𝐢𝐧 𝝅𝒙), 𝟎≤𝒙≤𝟑

By taking step size along 𝒙 − axis 𝒉 = 𝟎. 𝟏 and along 𝒕 − axis 𝒌 = 𝟎. 𝟏

Program:

function [ ] = oneD_heat(t0,tn,x0,xm,h,k,c2)
t=t0:k:tn;
x=x0:h:xm;
m=length(x);
n=length(t);
a=c2*k/h^2;
f=@(x) 2*(1+sin(pi*x)); %Change f according with the problem
u=zeros(m,n);
u(:,1)=f(x);
if a>0.5
fprintf('The method fails')
return
end
for j=1:n-1
for i=2:m-1
u(i,j+1)=a*u(i-1,j)+(1-2*a)*u(i,j)+a*u(i+1,j);
end
end
disp(u)
%surf(t,x,u)
for j=1:n
plot(x,u(:,j),'r')
hold on
pause
end
end
Output:
>> t0=0; tn=0.5; x0=0; xm=3; h=0.1; k=0.1; c2=1/20;

>> oneD_heat(t0,tn,x0,xm,h,k,c)

2.0000 0 0 0 0 0
2.6180 2.5878 1.5590 1.5317 1.2556 1.2309
3.1756 3.1180 3.0633 2.5113 2.4618 2.1647
3.6180 3.5388 3.4635 3.3919 3.0738 3.0090
3.9021 3.8090 3.7205 3.6363 3.5562 3.3550
4.0000 3.9021 3.8090 3.7205 3.6363 3.5562
3.9021 3.8090 3.7205 3.6363 3.5562 3.4800
3.6180 3.5388 3.4635 3.3919 3.3238 3.2590
3.1756 3.1180 3.0633 3.0113 2.9618 2.9147
2.6180 2.5878 2.5590 2.5317 2.5056 2.4809
2.0000 2.0000 2.0000 2.0000 2.0000 2.0000
1.3820 1.4122 1.4410 1.4683 1.4944 1.5191
0.8244 0.8820 0.9367 0.9887 1.0382 1.0853
0.3820 0.4612 0.5365 0.6081 0.6762 0.7410
0.0979 0.1910 0.2795 0.3637 0.4438 0.5200
0 0.0979 0.1910 0.2795 0.3637 0.4438
0.0979 0.1910 0.2795 0.3637 0.4438 0.5200
0.3820 0.4612 0.5365 0.6081 0.6762 0.7410
0.8244 0.8820 0.9367 0.9887 1.0382 1.0853
1.3820 1.4122 1.4410 1.4683 1.4944 1.5191
2.0000 2.0000 2.0000 2.0000 2.0000 2.0000
2.6180 2.5878 2.5590 2.5317 2.5056 2.4809
3.1756 3.1180 3.0633 3.0113 2.9618 2.9147
3.6180 3.5388 3.4635 3.3919 3.3238 3.2590
3.9021 3.8090 3.7205 3.6363 3.5562 3.4800
4.0000 3.9021 3.8090 3.7205 3.6363 3.5562
3.9021 3.8090 3.7205 3.6363 3.5562 3.3550
3.6180 3.5388 3.4635 3.3919 3.0738 3.0090
3.1756 3.1180 3.0633 2.5113 2.4618 2.1647
2.6180 2.5878 1.5590 1.5317 1.2556 1.2309
2.0000 0 0 0 0 0

3.5

2.5

1.5

0.5

0
0 0.5 1 1.5 2 2.5 3
7. Write a MATLAB program to solve the wave equation by explicit method.
𝝏𝒖 𝝏𝟐 𝒖
= , 𝒖(𝟎, 𝒕) = 𝟎, 𝒖(𝟑, 𝒕) = 𝟎, 𝟎≤𝒕≤𝟏
𝝏𝒕 𝝏𝒙𝟐
𝝏𝒖(𝒙, 𝟎)
𝒖(𝒙, 𝟎) = 𝒙(𝟏 − 𝒙) =𝟎 𝟎≤𝒙≤𝟏
𝝏𝒙
By taking step size along 𝒙 − axis, 𝒉 = 𝟏/𝟖 and along 𝒕 − axis, 𝒌 = 𝟏/𝟗

Here 𝒄𝟐 = 𝟏 implies 𝒄 = 𝟏, 𝒇 = 𝒙(𝟏 − 𝒙) and 𝒈 = 𝟎.

Program:
function [ ] = WaveEquation(t0,tn,x0,xm,h,k,c)
t=t0:k:tn;
x=x0:h:xm;
m=length(x);
n=length(t);
a=c*k/h;
f=@(x) x.*(1-x); % Change f according with the problem
g=@(x)0;
u=zeros(m,n);
u(:,1)=f(x);
if a>1
fprintf('The method fails')
return
end
for j=1:n-1
for i=2:m-1
if j==1
u(i,j+1)=(a^2*(u(i-1,j)+u(i+1,j))+(2-2*a^2)*u(i,j)+k*2*g(x(i)))/2;
else
u(i,j+1)=a^2*(u(i-1,j)+u(i+1,j))+(2-2*a^2)*u(i,j)-u(i,j-1);
end
end
end
disp(u)
surf(t,x,u)
end

output:

>> t0=0; tn=1; x0=0; xm=1; h=1/4; k=1/5; c=1;


>> WaveEquation(t0,tn,x0,xm,h,k,c)
0 0 0 0 0 0
0.1875 0.1475 0.0531 -0.0517 -0.1397 -0.1845
0.2500 0.2100 0.0900 -0.0772 -0.2117 -0.2541
0.1875 0.1475 0.0531 -0.0517 -0.1397 -0.1845
0 0 0 0 0 0

Note: Matrix displayed is displacement 𝑢 at different mesh points, 𝑖 𝑡ℎ column in the matrix represents
solution at (𝑖 − 1)𝑡ℎ time level for 𝑖 = {1, 2, 3, 4, 5, 6}.
0.4

0.2

-0.2

-0.4
1
1
0.8
0.5 0.6
0.4
0.2
0 0

Potrebbero piacerti anche