What's wrong with this code?
Show older comments
clear all
%%Initialization of Parameters
it = 0;
ft = 2;
h = 0.2;
iy = 0;
%%Derived Variables
numSteps = (ft - it)/h;
soln = zeros(1, numSteps+1);
soln(1) = iy;
timeAxis = [it: h: ft]
%%Anonymous Functions
f=@(y) 1+y^2;
%%For Loop
for i = 2:numSteps+1
soln(i) = soln(i-1) + h*(f(soln(i-1)));
end
%%Plot
figure(1)
plot(timeAxis, soln)
grid on
Every time this is run, the solutions run up exponentially and I know it's not correct. I'm not very familiar with the Euler method and I assume it's because I'm not utilizing it correctly. Any suggestions? I'm desperate
Accepted Answer
More Answers (0)
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!