0% found this document useful (0 votes)
53 views5 pages

Quiz 2

1) The document contains code that uses Euler's method and the Runge-Kutta method to numerically solve a differential equation and output tables of results. 2) The document uses polynomial fitting and spline interpolation to estimate voltage values at times not in the original dataset and compares the results. Plots of the original data, fitted polynomial, and spline interpolation are shown. 3) Code is presented to estimate the voltage after 15 days using a 4th order polynomial fit and spline interpolation to the original voltage dataset over time. The estimated values are printed.

Uploaded by

Aliza Burhanudin
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views5 pages

Quiz 2

1) The document contains code that uses Euler's method and the Runge-Kutta method to numerically solve a differential equation and output tables of results. 2) The document uses polynomial fitting and spline interpolation to estimate voltage values at times not in the original dataset and compares the results. Plots of the original data, fitted polynomial, and spline interpolation are shown. 3) Code is presented to estimate the voltage after 15 days using a 4th order polynomial fit and spline interpolation to the original voltage dataset over time. The estimated values are printed.

Uploaded by

Aliza Burhanudin
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy