why i am getting too many input arguments

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

 Accepted 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.

4 Comments

thank you so much. i have another code for the same question. if you dont mind,can you please check why this command does not display anything in the command window. thank you in advance %% Euler simple method close all clear all clc % f(x) = -0.5x^4+4x^3-10x^2+8.5x+1; need to find the derivative f= @(x) (-0.5*x.^4)+(4*x.^3)-(10*x.^2)+(8.5*x)+1; df = @(x) (-2*x.^3)+(12*x.^2)-(20*x)+8.5; xint = 0; xend = 4; %Interval h =0.5; %step size yint = 1; %Initial condition x=0, y=1 x = (xint:h:xend)'; ytrue = f(x); sh = df(x)*h; % f(xi,yi)h (slope*h) yeuler = yint; for i= 2:xend*2 yeuler(i) =sh(i-1)+ yeuler(i-1); end
My pleasure.
You did not say what command was not displaying. I notice that all your statements have a semicolon (;) at the end of the line. The semicolon will prevent the results of an assignment from displaying to the Command Window.
Try these lines in your script to see the effect:
str1 = 'This line WILL NOT display to the Command Window because it has a semicolon at the end';
str2 = 'This line WILL display to the Command Window because it does not have a semicolon at the end'
Please copy them exactly to your script. Delete them from your script after you have run them.
thank you so much. it worked.
As always, my pleasure.

Sign in to comment.

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!