Compute x and y by integrating the ODE system using Huen's method.
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
trying to code a function - see below - any help would be appreciated - trying to compute x and y by integrarting the ODE using Huens method but can use ODE solvers.
function [x,y] = odeHuen (f,x0,h,nSteps,y0)
% Initialize the output solution arrays.
m = length(y0);
x = zeros(1,nSteps+1);
y = zeros(m,nSteps+1);
x(1) = x0;
y(:,1) = y0;
here need code to Compute x and y by integrating the ODE system using Huen's method.
here is the call to action function:
f = @(x,y) [y(2); -y(1)]; % ODE function, note this trial code should give cos and sin plots
x0 = 0; h = 0.1; nSteps = 100; y0 = [1; 0]; % Initial condition and solver parameters
[x,y] = odeHuen(f,x0,h,nSteps,y0);
plot(x,y)
legend({'$\mathbf{y}_1$','$\mathbf{y}_2$'},'Interpreter','latex','FontSize',16)
0 comentarios
Respuestas (1)
darova
el 2 de Abr. de 2021
You should use for loop
function [x,y] = odeHuen (f,x0,h,nSteps,y0)
% Initialize the output solution arrays.
m = length(y0);
x = x0:h:nSteps*h;
y = zeros(2,nSteps+1);
x(1) = x0;
y(1) = y0;
for i = 1:nSteps-1
y(:,i+1) = y(:,i) + h*f(x(i),y(:,i));
y(:,i+1) = y(:,i) + h/2*(f(x(i),y(:,i) + f(x(i),y(:,i+1));
end
1 comentario
Louis Graham
el 24 de Mzo. de 2022
This returns an error message that says
File: solution.m Line: 12 Column: 39
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Do you know why?
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!