How Do I Plot 24 equally distanced points (from 0<t<5, inclusive) that agree with an exact solution to an IVP? (RK4/Runge Katta Method)
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Student4578
el 5 de Abr. de 2023
Hi all,
I have spent ages trying to figure this out. I have all the parts solve expect for 1b), part ii. My 24 points end right before t = 5, and I'm not sure if my points approximate the solution I obtained to the IVP well enough ( y(t) = (3t^2 + 1)^(1/3) ) . Any guidance would be appreciated! I also don't know how to convert my code into a "for" loop. The question doesn't ask for it, but I know it would be easier.
Here is my work so far!

%% a) Use Wolfram Alpha to obtain a function y that satisfies the differential
% equation and the initial condition. Include a screenshot of the
% results in your write up.
% Entering the given 1st order, nonlinear ODE into Wolfram Alpha, the exact
% solution satisfying the IVP (given the initial condition y(0) = 1) is the function
% y(t) = (3*t^2 + 1)^(1/3)
%% b) Perform the following in MATLAB.
%% i. Plot the solution obtained in part a) over t in [0,5]
t = linspace(0,5,100);
y = (3*t.^2 + 1).^(1/3);
plot(t,y)
ylim([0,4.5])
title('Plot of the Exact Solution y(t) = (3*t^2 + 1)^1/3 over [0,5]')
xlabel('t')
ylabel('y')
%% ii. Make use of the provided RK4 function in RK4.m to obtain iterates at
% N = 24 equidistant points, starting at 0 and ending at 5. Give a scatter
% plot of the iterates on top of the previous plot.
t_p = linspace(0,5,100);
y_p = (3*t_p.^2 + 1).^(1/3);
plot(t_p,y_p)
ylim([0,4.5])
hold on
f = @(t,y) ((2*t)/(y.^2));
h = 5/24; t = 0; u = 1;
u1 = RK4(f,h,t,u)
u2 = RK4(f,h,5/24,u1)
u3 = RK4(f,h,10/24,u2)
u4 = RK4(f,h,15/24,u3)
u5 = RK4(f,h,20/24,u4)
u6 = RK4(f,h,25/24,u5)
u7 = RK4(f,h,30/24,u6)
u8 = RK4(f,h,35/24,u7)
u9 = RK4(f,h,40/24,u8)
u10 = RK4(f,h,45/24,u9)
u11 = RK4(f,h,50/24,u10)
u12 = RK4(f,h,55/24,u11)
u13 = RK4(f,h,60/24,u12)
u14 = RK4(f,h,65/24,u13)
u15 = RK4(f,h,70/24,u14)
u16 = RK4(f,h,75/24,u15)
u17 = RK4(f,h,80/24,u16)
u18 = RK4(f,h,90/24,u17)
u19 = RK4(f,h,95/24,u18)
u20 = RK4(f,h,100/24,u19)
u21 = RK4(f,h,105/24,u20)
u22 = RK4(f,h,110/24,u21)
u23 = RK4(f,h,115/24,u22)
u24 = RK4(f,h,120/24,u23)
scatter([t 5/24 10/24 15/24 20/24 25/24 30/24 35/24 40/24 45/24 50/24 55/24 60/24 65/24 70/24 75/24 80/24 85/24 90/24 95/24 100/24 105/24 110/24 115/24],[u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24])
hold off
Here is the given RK4 function I have on file:
function unext = RK4(f, h, t, u)
k1 = h*f(t,u);
k2 = h*f(t+h/2, u+k1/2);
k3 = h*f(t+h/2, u+k2/2);
k4 = h*f(t+h, u+k3);
k4 = h*f(t+h, u+k3);
unext = u+(k1+2*k2+2*k3+k4)/6;
end
And here is my plot, along with my scatter plot of points. The plot is correct, but the scatter plot of 24 points isn't (don't end at t=5, and seem to get less accurate as t approaches 5)... How do i fix this using the RK4 function that was given?

Thank you!
0 comentarios
Respuesta aceptada
Torsten
el 5 de Abr. de 2023
Editada: Torsten
el 5 de Abr. de 2023
Use
scatter([t 5/24 10/24 15/24 20/24 25/24 30/24 35/24 40/24 45/24 50/24 55/24 60/24 65/24 70/24 75/24 80/24 85/24 90/24 95/24 100/24 105/24 110/24 115/24 120/24],[u u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24])
instead of
scatter([t 5/24 10/24 15/24 20/24 25/24 30/24 35/24 40/24 45/24 50/24 55/24 60/24 65/24 70/24 75/24 80/24 85/24 90/24 95/24 100/24 105/24 110/24 115/24],[u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24])
And yes: A for-loop would be easier.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Ordinary Differential Equations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!