Problem with Forward Euler function?
显示 更早的评论
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.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!