You are on page 1of 5

1) A.

%Quiz 2 Part1a Caleb J. Petersen


clear all
clc
dy = @(x,y) (1+2*x)*sqrt(y);
fprintf('Part a. table\n')
fprintf('x
| Y_Euler
|\n')
fprintf('--------------------------------------------\n')
y(1) = 2;
x(1)=0;
k=1;
h = .25;
x_max = 1;
while x(k)<=x_max;
y(k+1) = y(k) + dy(x(k),y(k))*h;
x(k+1) = x(k) + h;
fprintf('%2.3f
| %3.5f |\n',x(k),y(k))
k=k+1;
end

Part a. table
x
| Y_Euler
|
-------------------------------------------0.000
| 2.00000 |
0.250
| 2.35355 |
0.500
| 2.92885 |
0.750
| 3.78455 |
1.000
| 5.00042 |

B.

%Quiz 2 Part1b
Caleb J. Petersen
clear all
clc
dy = @(x,y) (1+2*x)*sqrt(y);
fprintf('Part b. table\n')
fprintf('x
| Y_RK
|\n')
fprintf('--------------------------------------------\n')
y(1) = 2;
x(1)=0;
k=1;
k_1=0;
k_2=0;
k_3=0;
k_4=0;
h = .25;
x_max = 1;
while x(k)<=x_max;
k_1=h*dy(x(k),y(k));
k_2=h*dy(x(k)+1/2*h,y(k)+1/2*k_1);
k_3=h*dy(x(k)+1/2*h,y(k)+1/2*k_2);
k_4=h*dy(x(k)+h,y(k)+k_3);
y(k+1)=y(k)+1/6*(k_1+2*k_2+2*k_3+k_4);
x(k+1) = x(k) + h;
fprintf('%2.3f
| %3.5f |\n',x(k),y(k))
k=k+1;
end
fprintf('==================================================\n')

Part b. table
x
| Y_RK
|
-------------------------------------------0.000
| 2.00000 |
0.250
| 2.46635 |
0.500
| 3.20126 |
0.750
| 4.28676 |
1.000
| 5.82833 |
==================================================

2)
%Problem 2 no. (1).
t_plot=0:.25:30;
t=[5,9,13,17,21,25,29];
v=[-7,-4,3.5,20,57,138,320];
k=1;
v_4plot=0;
v_4at15 = 0;
p_4=polyfit(t,v,4);
while k<=5;
v_4plot=v_4plot + p_4(k)*t_plot.^(5-k);
k=k+1;
end
k=1;
while k<=5;
v_4at15=v_4at15 + p_4(k)*15^(5-k);
k=k+1;
end
fprintf('Voltage at 15 days estimated using 4th order polynomial to\n')
fprintf('be %3.3d [[V]].\n',v_4at15)
%Problem 2 no. (2).
v_spline = interp1(t,v,t_plot,'spline');
v_spline15 = interp1(t,v,15,'spline');
fprintf('Voltage at 15 days estimated using spline to\n')
fprintf('be %3.3d [[V]].\n',v_spline15)
%Problem 2 no. (3).
figure(1)
plot(t,v,'+',t_plot,v_4plot,t_plot,v_spline)
xlabel('Time [[days]]')
ylabel('Voltage [[V]]')
title('Voltage over Time')

legend('Raw data','4th order approximation','Spline interpolation')

Voltage at 15 days estimated using 4th order polynomial to


be 1.142e+001 [[V]].
Voltage at 15 days estimated using spline to
be 1.004e+001 [[V]].

Voltage over Time


400
Raw data
4th order approximation
Spline interpolation
350

300

250

Voltage [[V]]

200

150

100

50

-50
0

10

15
Time [[days]]

20

25

30

You might also like