Debug code midpoint method
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Im not sure how to create a string with a function that has two variables. Can somebody tell me what im doing wrong dy/dt=t*e^(3t) - 2*y o<t<1 y(0)=0 nstep = 10
function fx = f(t,y)
fx=@(t,y)-y - sin(t) + cos(t);
return;
function [t,y]=midpoint(f,a,b,ya,nsteps)
% The Midpoint Method (a second order Runge-Kutta method) for OVD IVP's, dy/dt=f(t,y) y(a)=ya, a <= t <= b.
% Input Variables
% - f is a pointer to the function f(t,y). See usage note.
% - a, b specify the initial and final t values.
% - nsteps is the number of time steps to take, so stepsize
% dt=(b-a)/nsteps [a.k.a. h]
%
% Output Variables
% - t is the array of time values, a:dt:b
% - y is the array of corresponding approximate values of the solution y(t) [a.k.a. w_i]
t=zeros(nsteps+1,1);
% Yes, t is determined in advance as a:dt:b, but I alow for future modifications in which dt is not constant.
y=zeros(nsteps+1,1);
dt=(b-a)/nsteps;
% Use scalar variables for the current tnow and ynow vaues, then store into arrays too.
tnow=a;
ynow=ya;
t(1)=tnow;
y(1)=ynow;
for step=1:nsteps
k1=dt*f(tnow,ynow);
k2=dt*f(tnow+dt/2,ynow+k1/2);
ynow=ynow+k2;
tnow=tnow+dt;
y(step+1)=ynow;
t(step+1)=tnow;
end
0 comentarios
Respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!