You are on page 1of 15

Faculty of Mathematical and Physical Sciences

Department of Mathematics and Statistics

Solutions of differential equations.


If you have any issues in running the following codes, contact me
at: sumanth.mt.mp@msruas.ac.in

Q1.
Consider the IVP:
𝒅𝒚
= 𝟒𝒆𝟎.𝟖𝒙 − 𝟎. 𝟓𝒚, 𝒚(𝟎) = 𝟐.
𝒅𝒙
a) Write a MATLAB function to solve the IVP using Euler’s method in the interval [𝟎, 𝟒]. Take step
size 𝒉 = 𝟏 and record the output.
b) Determine the exact solution using suitable MATLAB built-in function.
c) Plot the exact and numerical solution in the interval [𝟎, 𝟒].

Program 1 a): Euler’s Method


function [x,y] = EulerODE(x0, xn, y0, h, f)
x=x0:h:xn;
n=length(x)-1;
y=zeros(1,n+1);
y(1) = y0;
for j=1:n
y(j+1) = y(j) + h*f(x(j), y(j));
end
end
Note: To get the output, you can write the following commands in a
script file or in command window. But I prefer a script, since it can
modified easily and used again for other functions. If you write a
script, keep the script file and function file in the same folder.

Program 1 b): Script1


Note: First 2 lines and the dsolve data of the script has to be changed according to the given
problem.
x0=0; xn = 4; y0=2; h=1;
f = @(x,y) 4*exp(0.8*x) - 0.5*y; % Change according to the problem.
[x,y] = EulerODE(x0, xn, y0, h, f);
fprintf('Approximate solution of the ODE is:\n');
disp([x;y]);
ye=eval(dsolve('Dy=4*exp(0.8*x) - 0.5*y ','y(0)=2.0','x'));% Change
according to the problem
plot(x,y,'*',x,ye,'r');
grid on;
xlabel('x - axis');
ylabel('y - axis');
title('Solution of IVP using Euler''s Method');
legend('Numerical solution', 'Exact Solution');
Output:
>> Script1
Approximate solution of the ODE is:
0 1.0000 2.0000 3.0000 4.0000
2.0000 5.0000 11.4022 25.5132 56.8493

Solution of IVP using Euler's Method


80

70

60

Numerical solution
50
Exact Solution
y - axis

40

30

20

10

0
0 0.5 1 1.5 2 2.5 3 3.5 4
x - axis

Q2.
Consider the IVP:
𝒅𝒚
= 𝒙𝟑 𝒚 − 𝟏. 𝟓𝒚, 𝒚(𝟎) = 𝟏.
𝒅𝒙
a) Write a MATLAB function to solve the IVP using modified Euler’s method in the interval [𝟎, 𝟐].
Take step size 𝒉 = 𝟎. 𝟏 and record the output.
b) Determine the exact solution using suitable MATLAB built-in function.
c) Plot the exact and numerical solution in the interval [𝟎, 𝟐].
Program 2 a): Euler’s Modified Method
function [x,y] = modEulerODE(x0, xn, y0, h, f)
x=x0:h:xn;
n=length(x)-1;
y=zeros(1,n+1);
y(1) = y0;
for j=1:n
yp = y(j) + h*f(x(j), y(j));
y(j+1) = y(j) + 0.5*h*( f(x(j), y(j)) + f(x(j+1), yp));
end
end
Program 2 b): Script2
Note: First 2 lines and the dsolve data of the script has to be changed according to the given
problem.
x0=0; xn = 2; y0=1; h=0.1;
f = @(x,y) 4*exp(0.8*x) - 0.5*y;
[x,y] = modEulerODE(x0, xn, y0, h, f);
fprintf('Approximate solution of the ODE is:\n');
disp([x;y]);
ye=eval(dsolve('Dy=4*exp(0.8*x) - 0.5*y ','y(0)=2.0','x'));% Change
according to the problem
plot(x,y,'*',x,ye,'r');
grid on;
xlabel('x - axis');
ylabel('y - axis');
title('Solution of IVP using modified Euler''s Method');
legend('Numerical solution', 'Exact Solution');

Output:
>> Script2
Approximate solution of the ODE is:
Columns 1 through 12

0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000


1.0000 0.8500 0.7226 0.6148 0.5242 0.4489 0.3872
0.7000 0.8000 0.9000 1.0000 1.1000
0.3375 0.2984 0.2690 0.2482 0.2358

Columns 13 through 21

1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000


0.2318 0.2371 0.2536 0.2852 0.3387 0.4266 0.5722
1.9000 2.0000
0.8200 1.2595

Solution of IVP using modified Euler's Method


3

2.5

2 Numerical solution
Exact Solution
y - axis

1.5

0.5

0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x - axis
Q3.
Consider the IVP:
𝒅𝒚
− 𝒚(𝟏 − 𝟐𝒙) = 𝟎, 𝒚(𝟎) = 𝟏.
𝒅𝒙
a) Write a MATLAB function to solve the IVP using Runge-Kutta 4th order method in the
interval [𝟎, 𝟑]. Take step size 𝒉 = 𝟎. 𝟏 and record the output.
b) Determine the exact solution using suitable MATLAB built-in function.
c) Plot the exact and numerical solution in the interval [𝟎, 𝟑].

Program 3 a): Runge-Kutta Fourth Order Method


function [x,y]=RK4ODE(x0,xn, y0,h,f )
x = x0:h:xn;
n = length(x)-1;
y = zeros(1,n+1);
y(1) = y0;
for i=1:n
K1 = h*f(x(i),y(i));
K2 = h*f(x(i)+0.5*h,y(i)+0.5*K1);
K3 = h*f(x(i)+0.5*h,y(i)+0.5*K2);
K4 = h*f(x(i)+h,y(i)+K3);
y(i+1) = y(i)+(1/6)*(K1+2*K2+2*K3+K4);
end
end

Program 3 b): Script3


Note: First 2 lines and the dsolve data of the script has to be changed according to the given
problem.
x0=0; xn = 3; y0=1; h=0.1;
f = @(x,y) y*(1-2*x);
[x,y]=RK4ODE(x0,xn, y0,h,f );
fprintf('Approximate solution of the ODE is :\n');
disp([x;y]);
ye=eval(dsolve('Dy=y*(1-2*x) ','y(0)=1.0','x'));% Change according to
the problem
plot(x,y,'*',x,ye,'r');
grid on;
xlabel('x - axis');
ylabel('y - axis');
title('Solution of IVP using RK-4 method');
legend('Numerical solution', 'Exact Solution');

Output:
>> Script3
Approximate solution of the ODE is :
Columns 1 through 12
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000
1.0000 1.0940 1.1732 1.2333 1.2708 1.2835 1.2706
0.7000 0.8000 0.9000 1.0000 1.1000
1.2330 1.1729 1.0936 0.9995 0.8956
Columns 13 through 24
1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000
0.7867 0.6775 0.5721 0.4737 0.3846 0.3063 0.2393
1.9000 2.0000 2.1000 2.2000 2.3000
0.1834 0.1379 0.1018 0.0737 0.0524
Columns 25 through 31
2.4000 2.5000 2.6000 2.7000 2.8000 2.9000 3.0000
0.0366 0.0251 0.0169 0.0112 0.0073 0.0047 0.0029

>>
Solution of IVP using RK-4 method
1.4

1.2

Numerical solution
1
Exact Solution

0.8
y - axis

0.6

0.4

0.2

0
0 0.5 1 1.5 2 2.5 3
x - axis

Q4.
Consider the IVP:
𝒅𝒚 𝟐𝒚 𝒚𝟐
=− − 𝟐, 𝒚(𝟏) = 𝟏.
𝒅𝒙 𝒙 𝒙
a) Write a MATLAB function to solve the IVP using Milne’s method in the interval [𝟏, 𝟓]. Obtain
the starting values using Runge-Kutta 4th order method. Take step size 𝒉 = 𝟎. 𝟐𝟓 and record
the output.
b) Determine the exact solution using suitable MATLAB built-in function.
c) Plot the exact and numerical solution in the interval [𝟏, 𝟓].
Program 4 a): Milne’s Method
function [x,y]=MilneODE(x0,y0,xn,h,f )
x=x0:h:xn;
n=length(x)-1;
y=zeros(1,n+1);
y(1)=y0;
for i=1:3
K1=h*f(x(i),y(i));
K2=h*f(x(i)+0.5*h,y(i)+0.5*K1);
K3=h*f(x(i)+0.5*h,y(i)+0.5*K2);
K4=h*f(x(i)+h,y(i)+K3);
y(i+1)=y(i)+(1/6)*(K1+2*K2+2*K3+K4);
end
for i=4:n
y(i+1)=y(i-3)+(4*h/3)*(2*f(x(i),y(i)) - f(x(i-1),y(i-1))+...
2*f(x(i-2),y(i-2))); %predictor formula
y(i+1)=y(i-1)+(h/3)*(f(x(i+1),y(i+1)) + 4*f(x(i),y(i)) +...
f(x(i-1),y(i-1))); % Corrector formula
end
end

Program 4 b): Script4


Note: First 2 lines and the dsolve data of the script has to be changed according to the given
problem.
x0=1; xn = 5; y0=1; h=0.25;
f = @(x,y) -2*y./x - y.^2./x.^2;
[x,y]=MilneODE(x0,y0,xn,h,f );
fprintf('Approximate solution of the ODE is\n');
disp([x;y]);
ye=eval(dsolve('Dy=-2*y/x - y^2/x^2', 'y(1)=1.0','x'));% Change
according to the problem
plot(x,y,'*',x,ye,'r');
grid on;
xlabel('x - axis');
ylabel('y - axis');
title('Solution of IVP using Milne''s Method');
legend('Numerical solution', 'Exact Solution');

Output:
>> Script4
Approximate solution of the ODE is
Columns 1 through 12

1.0000 1.2500 1.5000 1.7500 2.0000 2.2500 2.5000


2.7500 3.0000 3.2500 3.5000 3.7500
1.0000 0.5502 0.3599 0.2568 0.1877 0.1523 0.1163
0.1022 0.0789 0.0740 0.0565 0.0566

Columns 13 through 17

4.0000 4.2500 4.5000 4.7500 5.0000


0.0421 0.0451 0.0321 0.0372 0.0249
Solution of IVP using Milne's Method
1
Numerical solution
0.9 Exact Solution

0.8

0.7

0.6
y - axis

0.5

0.4

0.3

0.2

0.1

0
1 1.5 2 2.5 3 3.5 4 4.5 5
x - axis

Q5.
Consider the 1-dimensional heat equation:
𝝏𝒖 𝝏𝟐 𝒖
=𝟐 𝟐 , 𝟎 < 𝒙 < 𝟓, 𝒕 > 𝟎,
𝝏𝒕 𝝏𝒙
subject to the conditions:
𝒖(𝟎, 𝒕) = 𝟎, 𝒖(𝟓, 𝒕) = 𝟎, 𝒕 ≥ 𝟎;
𝒖(𝒙, 𝟎) = 𝒙𝟐 (𝟐𝟓 − 𝒙𝟐 ), 𝟎 ≤ 𝒙 ≤ 𝟓.
a) Write a MATLAB program to approximate the solution 𝐮(𝐱, 𝐭) for 5 time levels using the
Schmidt’s explicit method. Take the step size 𝐡 = 𝟎. 𝟓 along 𝐱-axis and 𝐤 = 𝟎. 𝟎𝟓 along t-
axis.
b) Plot the numerical solution in the domain 𝟎 ≤ 𝐭 ≤ 𝟎. 𝟐𝟓 𝐚𝐧𝐝 𝟎 ≤ 𝐱 ≤ 𝟓.
Note: Here we take 𝒄𝟐 = 𝟐 = 𝒄𝟐, 𝒖(𝒙, 𝟎) = 𝒙𝟐 (𝟐𝟓 − 𝒙𝟐 ) = 𝒇(𝒙). The final
time 𝒕𝒎 calculated as is 5 times 𝒌 (i.e. number of time levels ×
step size along 𝒕 −axis.)
Program 5: 1-D Heat equation
function [x,t,u] = FTCS_Heat(x0,xm,tn,h,k,c2,f)
lambda=c2*k/h^2;
x=x0:h:xm;
n=length(x);
t=0:k:tn;
m=length(t);
u=zeros(n,m);
u(:,1)=f(x);
for j=1:m-1
for i=2:n-1
u(i,j+1)= lambda*u(i-1,j)+(1-2*lambda)*u(i,j)+lambda*u(i+1,j);
end
end
end

Program 5 b): Script5


Note: First 2 lines of the script has to be changed according to the given problem.
x0=0; xm = 5; tn = 0.25; h = 0.5; k=0.05; c2=2;
f = @(x) x.^2.*(25-x.^2);
[x,t,u] = FTCS_Heat(x0,xm,tn,h,k,c2,f);
fprintf('Numerical solution given equation is : \n');
disp(u);
plot(x,u);
grid on;
xlabel('x - values');
ylabel('Solution at specified t - values');
title('Numerical solution of 1-D Heat equation');

Output:
>> Script5
Numerical solution given equation is :
0 0 0 0 0 0
6.1875 10.8375 13.2675 15.1575 16.5267 17.5844
24.0000 27.7500 31.2600 33.7380 35.6976 37.1225
51.1875 53.4375 55.4475 57.2175 58.4307 59.2292
84.0000 84.1500 84.0600 83.7300 83.1600 82.2233
117.1875 114.6375 111.8475 108.8175 105.5475 102.0375
144.0000 138.1500 132.0600 125.7300 119.1600 112.9913
156.1875 146.4375 136.4475 126.2175 117.3507 109.2212
144.0000 129.7500 115.2600 104.5380 95.2176 87.4745
96.1875 76.8375 67.2675 59.5575 53.7267 48.8324
0 0 0 0 0 0
Numerical solution of 1-D Heat equation
160

140

120
Solution at specified t - values

100

80

60

40

20

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
x - values
Q6
Consider the 1-dimensional wave equation:
𝝏𝟐 𝒖 𝝏𝟐 𝒖
=𝟒 𝟐 , 𝟎 < 𝒙 < 𝟏, 𝒕 > 𝟎,
𝝏𝒕𝟐 𝝏𝒙
subject to the conditions:
𝒖(𝟎, 𝒕) = 𝟎, 𝒖(𝟏, 𝒕) = 𝟎, 𝒕 ≥ 𝟎;
𝒖(𝒙, 𝟎) = 𝐬𝐢𝐧 𝟐𝝅𝒙, 𝒖𝒕 (𝒙, 𝟎) = 𝟎, 𝟎 ≤ 𝒙 ≤ 𝟏.
c) Write a MATLAB program to approximate the solution 𝐮(𝐱, 𝐭) for 5 time levels using the
finite difference method. Take the step size 𝐡 = 𝟎. 𝟎𝟓 along 𝐱-axis and 𝐤 = 𝟎. 𝟏 along t-axis.
d) Plot the numerical solution in the domain 𝟎 ≤ 𝐭 ≤ 𝟎. 𝟓 𝐚𝐧𝐝 𝟎 ≤ 𝐱 ≤ 𝟏.
Note: Here we take 𝒄𝟐 = 𝟒 ⇒ 𝒄 = 𝟐, 𝒖(𝒙, 𝟎) = 𝐬𝐢𝐧 𝝅𝒙 = 𝒇(𝒙) and
𝒖𝒕 (𝒙, 𝟎) = 𝟎 = 𝒈(𝒙). The final time 𝒕𝒎 calculated as is 5 times 𝒌 (i.e.
number of time levels × step size along 𝒕 −axis.
Program 6 a): 1-D Wave equation
function [x,t,u] = CTCS_wave(t0,tm,x0,xn,h,k,c,f,g)
r = c*k/h;
x=x0:h:xn;
t=t0:k:tm;
n=length(x);
m=length(t);
u=zeros(n,m);
u(:,1)=f(x);
for i=2:n-1
u(i,2)=(1-r^2)*u(i,1)+0.5*(r^2*(u(i-1,1)+u(i+1,1)))+k*g(x(i));
end
for j=2:m-1
for i=2:n-1
u(i,j+1)=2*(1-r^2)*u(i,j)+r^2*(u(i-1,j)+u(i+1,j))-u(i,j-1);
end
end
end

Program 6 b): Script6


Note: First 2 lines of the script has to be changed according to the given problem.
x0=0; xn=1; t0=0; tm=0.5; h=0.05; k=0.1; c=1;
f= @(x) sin(2*pi*x); g = @(x) 0;
[x,t,u] = CTCS_wave(t0,tm,x0,xn,h,k,c,f,g);
fprintf('Numerical solution given equation is : \n');
disp(u);
plot(x,u);
grid on;
xlabel('x - values');
ylabel('Solution at specified t - values');
title('Numerical solution of 1-D wave equation');

Output:
>> Script5
Numerical solution given equation is :
0 0 0 0 0 0
0.3090 0.2485 0.0907 -0.1026 -0.2558 -0.3088
0.5878 0.4727 0.1725 -0.1952 -0.4865 -0.5873
0.8090 0.6506 0.2375 -0.2686 -0.6696 -0.8084
0.9511 0.7649 0.2792 -0.3158 -0.7871 -0.9503
1.0000 0.8042 0.2936 -0.3321 -0.8276 -0.9992
0.9511 0.7649 0.2792 -0.3158 -0.7871 -0.9503
0.8090 0.6506 0.2375 -0.2686 -0.6696 -0.8084
0.5878 0.4727 0.1725 -0.1952 -0.4865 -0.5873
0.3090 0.2485 0.0907 -0.1026 -0.2558 -0.3088
0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000
-0.3090 -0.2485 -0.0907 0.1026 0.2558 0.3088
-0.5878 -0.4727 -0.1725 0.1952 0.4865 0.5873
-0.8090 -0.6506 -0.2375 0.2686 0.6696 0.8084
-0.9511 -0.7649 -0.2792 0.3158 0.7871 0.9503
-1.0000 -0.8042 -0.2936 0.3321 0.8276 0.9992
-0.9511 -0.7649 -0.2792 0.3158 0.7871 0.9503
-0.8090 -0.6506 -0.2375 0.2686 0.6696 0.8084
-0.5878 -0.4727 -0.1725 0.1952 0.4865 0.5873
-0.3090 -0.2485 -0.0907 0.1026 0.2558 0.3088
-0.0000 0 0 0 0 0
Numerical solution of 1-D wave equation
1

0.8

0.6
Solution at specified t - values

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x - values
Built-in MATLAB functions to solve ODE’s and curve-fitting.
Q7.
Consider the IVP:
𝒅𝒚
= 𝟒𝒆𝟎.𝟖𝒙 − 𝟎. 𝟓𝒚, 𝒚(𝟎) = 𝟐.
𝒅𝒙
a) Write a MATLAB script to solve the given IVP in the interval [𝟎, 𝟒] numerically, using a suitable
built-in MATLAB function .
b) Plot the numerical solution in the interval [𝟎, 𝟒].
Note: Here we have to use the MATLAB built-in function ode45, which gives us the required
numerical solution. First 3 lines of the script has to be written according to the given problem.
Program 7 : Script7
Note: First 3 lines of the script has to be changed according to the given problem.
f = @(x,y) 4*exp(0.8*x) - 0.5*y;
xspan = [0 4];
y0 = 2;
[x,y] = ode45(f, xspan, y0);
plot(x,y,'*-');
grid on;
xlabel('x-axis');
ylabel('y-axis');
title('Solution of IVP using ode45');

Output:
>> Script7

Solution of IVP using ode45


80

70

60

50
y-axis

40

30

20

10

0
0 0.5 1 1.5 2 2.5 3 3.5 4
x-axis
The function 𝒐𝒅𝒆𝟒𝟓 can be used to solve higher order ODE’s as well. For example,
Q8.
Consider the IVP:
𝒚′′ − 𝟐𝒚′ + 𝒚 = −𝟐, 𝒚(𝟎) = 𝒚′ (𝟎) = 𝟏.

a) Write a MATLAB script to solve the given IVP in the interval [𝟎, 𝟏] numerically, using a suitable
built-in MATLAB function .
b) Plot the numerical solution in the interval [𝟎, 𝟏].
Note: First we convert the given 2nd order ODE into a system of 2 first order ODE’s. Then we use
the MATLAB built-in function ode45, to obtain the required numerical solution.
For this, make the substitution 𝒚 = 𝒚𝟏 , 𝒚′𝟏 = 𝒚𝟐 ⇒ 𝒚′′ ′
𝟏 = 𝒚𝟐 . Hence the given IVP reduces to:

𝒚′𝟏 = 𝒚𝟐 , 𝒚𝟏 (𝟎) = 𝟏,
𝒚′𝟐 = −𝟐 − 𝒚𝟏 + 𝟐𝒚𝟐 , 𝒚𝟐 (𝟎) = 𝟏.
This method works for any 𝒏𝒕𝒉 order ODE. We will convert it into 𝒏 first order ODE’s.
(𝒏)
i.e. 𝒚 = 𝒚𝟏 , 𝒚′𝟏 = 𝒚𝟐 , 𝒚′𝟐 = 𝒚𝟑 , ⋯ 𝒚′𝒏−𝟏 = 𝒚𝒏 ⇒ 𝒚(𝒏) = 𝒚𝟏 = 𝒚𝒏 , and solve the given equation
for 𝒚𝒏 .
Program 8 : Script8
Note: First 3 lines of the script has to be changed according to the given problem.
f=@(x,y) [y(2); 2*y(2)-y(1)-2];
xspan = [0,1];
y0=[1;1];
[x,y]=ode45(f,xspan,y0);
plot(x,y(:,1),'*-');
grid on
xlabel('x-axis');
ylabel('y-axis');
title('Solution of IVP using ode45');

Output:
>> Script8

Solution of IVP using ode45


1.4

1.3

1.2

1.1
y-axis

0.9

0.8

0.7
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x-axis
Q9.
Consider the IVP:
𝒅𝒚
= 𝟒𝒆𝟎.𝟖𝒙 − 𝟎. 𝟓𝒚, 𝒚(𝟎) = 𝟐.
𝒅𝒙
a) Write a MATLAB script to find analytical (exact) solution of the given IVP using a suitable built-
in MATLAB function .
b) Plot the obtained solution in the interval [𝟎, 𝟑].
Program 9 : Script9
Note: First 2 lines of the script has to be changed according to the given problem.
x=linspace(0,3);
ye=dsolve('Dy=y*(1-2*x)', 'y(0)=1.0','x');
fprintf('The solution of the given IVP is:\n');
disp(ye);
y=eval(ye);
plot(x,y,'*-');
grid on;
xlabel('x - axis');
ylabel('y - axis');
title('Solution of IVP using dsolve');

Output:
>> Script9
Solution of IVP using dsolve
1.4

1.2

0.8
y - axis

0.6

0.4

0.2

0
0 0.5 1 1.5 2 2.5 3
x - axis
The built-in MATLAB function polyfit can be used fit a polynomial to a given data set using the
method of least squares. For example,
Q10:
The results of measurements of electric resistance R of a copper bar at various temperatures
𝒕∘ 𝑪 are listed below:
𝒕 19 25 30 36 40 45 50
𝑹 76 77 79 80 82 83 85

a) Write a MATLAB script to find a relation 𝑹 = 𝒂𝒕 + 𝒃 by using a suitable built-in MATLAB


function .
b) Plot the data and the obtained linear fit in a single graph.
Program 9 : Script9
Note: First 2 lines of the script has to be changed according to the given problem.
x = [19 25 30 36 40 45 50];
y = [76 77 79 80 82 83 85 ];
p = polyfit(x,y,1);
fprintf('The least squares linear fit is: y = %f x + %f. \n',p(1),p(2));
f = @(x) p(1)*x+p(2);
plot(x,y,'*',x,f(x),'r');
grid on;
xlabel('x-axis');
ylabel('y-axis');
title('Graph of least squares linear fit');
legend('data','Linear fit','Location','Best');

Output:
>> Script10
The least squares linear fit is: y = 0.292350 x + 70.053474.
Graph of least squares linear fit
85
data
84 Linear fit

83

82

81
y-axis

80

79

78

77

76

75
15 20 25 30 35 40 45 50
x-axis
Q11:
The voltage 𝑽 across a capacitor at time 𝒕 seconds is given by the following table:
𝒕 0 2 4 6 8
𝑽 150 63 28 12 5.6

a) Write a MATLAB script to find a relation 𝑽 = 𝒂𝒆𝒃𝒕 by using a suitable built-in MATLAB function.
b) Plot the data and the obtained linear fit in a single graph.
Note: We convert the problem of fitting an exponential curve into a problem of fitting a linear curve
as follows. Taking logarithm of 𝑽 = 𝒂𝒆𝒃𝒕 , we get 𝐥𝐨𝐠 𝑽 = 𝐥𝐨𝐠 𝒂 + 𝒃𝒕. Let 𝒚 = 𝐥𝐨𝐠 𝑽, 𝐀 = 𝐥𝐨𝐠 𝒂.
Then the line we have to fit is 𝒚 = 𝑨 + 𝒃𝒕.

Program 11 : Script11
Note: First 2 lines of the script has to be changed according to the given problem.
x = [0 2 4 6 8];
y = [150 63 28 12 5.6];
Y = log(y);
p = polyfit(x,Y,1);
f = @(x) exp(p(1)*x+p(2));
fprintf('The exponential fit is: y = %f exp(%f x). \n',exp(p(2)), p(1));
plot(x,y,'*',x,f(x),'r');
grid on;
xlabel('x-axis');
ylabel('y-axis');
title('Graph of least squares exponential fit');
legend('data','Linear fit','Location','Best');

Output:
>> Script11
The least squares exponential fit is: y = 146.280008 exp(-0.411698 x).

Graph of least squares exponential fit


150
data
Linear fit

100
y-axis

50

0
0 1 2 3 4 5 6 7 8
x-axis

 THE END 

You might also like