Solving a linear system of 1st order ODEs using the Forward Euler method and ode45
Afficher commentaires plus anciens
I need to solve a linear system of 1st order ODEs using the Forward Euler method and ode45.
The equations to solve:
dxdt = (-1/2)*x+y;
dydt = x+(1/3)*y;
this is what I have so far:
xf = 4; %final time
xspan = [0,xf];
h = .05; %stepsize
N = xf/h;
Y0 = [6,-4.2];
myx = zeros(N+1,1);
myY = zeros(N+1,2);
myY(1,:) = Y0;
% Integrate using Forward Euler
for k = 1:N
myY(k+1,:) = myY(k,:) +...
h*(jump(myx(k),myY(k,:)))';
myx(k+1) = myx(k) + h;
end
% Integrate using ode45
[x,y] = ode45(@jump,xspan,Y0);
plot(x,y(:,2),'ko',myx(1:skip:end),myY(1:skip:end,2),'r*')
title('Trajectory of an object');
xlabel('Time (s)'); ylabel('Position (m)');
legend('ode45','forward Euler')
function dxdt = jump(x,y)
dxdt = [-(1/2)*x+y;x+(1/3)*y];
end
Error I'm getting:
Unable to perform assignment because the size of the left side is 1-by-2 and the size of the
right side is 2-by-2.
Error (line 15)
myY(k+1,:) = myY(k,:) +...
I also need different initial positions for both methods, (6m, -4.2m) for Forward Euler and (6m, -3.8m) for the ode45 and I'm not sure how to implement these
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Numerical Integration and Differential Equations dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!