MATLAB: Why i am getting too many input arguments

euler method

function EulersMethod
%Eulers method
%y' = -2x^3+12x^2-20x+8.5 from x = [0 4] y(1) = 1 and h=0.5
a = 0; %Interval we are using, from a to b.
b = 0.5;
y(1) = 1; %Initial value of y, i.e. y = 1 for x= 0.
N = 9; %Number of steps we will use
t(1) = a; %inital time we will start at
h = (b - a) / N; %Step size
f= @(x) (-0.5*x.^4)+(4*x.^3)-(10*x.^2)+(8.5*x)+1; %model we are using
for i=1:N
y(i+1) = y(i) + h*f(y(i), t(i)); %Implementation of Eulers method
t(i+1) = a + i*h;
end

Best Answer

The reason is that you have defined ‘f’ to be a function of ‘x’ only:
f= @(x) (-0.5*x.^4)+(4*x.^3)-(10*x.^2)+(8.5*x)+1; %model we are using
and in the ‘i’ loop, you are calling it with 2 arguments, ‘y(i)’ and ‘t(i)’:
y(i+1) = y(i) + h*f(y(i), t(i)); %Implementation of Eulers method
I leave the solution to you.