Problem with Forward Euler function?
Mostrar comentarios más antiguos
I have created a function using the forward Euler's method to approximate the solution to the ODE dy/dx(x)=f(x,y(x)). The only problem is that the function only works when y initial is a single value. I need it to work for when y initial is an array of values, but I'm not sure how I'm supposed to fix it. Here's my code:
function [x,y] = forwardEuler(f,a,b,n,y0)
h = (b-a)/n;
x = [a zeros(1,n)];
y = [y0 zeros(length(y0),n)];
for i = 1:n
x(i+1) = x(i)+h;
y(i+1) = y(i)+h*f(x(i),y(i));
end
end
I figured that the part that needs to be modified is where y is defined in the for loop, since y(i+1) assumes y will be a single column vector, so I tried changing it so that it counts for the actual size of y, but I'm kind of lost there.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Programming en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!