In what I missed

function [ val, ea, iter] = IterMeth(x , es, maxit)
%%initialization
iter=12;
val = exp(1);
xr = [ ];
ea =100;
%iterative calculation
while (1)
xrold=xr;
xr= xr-x^iter/factorial(iter); <--------------------------here?
iter= iter+ 1;
if xr~=0
ea=abs((xr-xrold)/xr)*100;
end
if ea<=es || iter >= maxit,break,end
end
val=xr;
end

8 Comments

What is x in IterMeth(x,es,maxit) ? Vector of values or scalar ?
If it's vector of values then use below
%true
xr-x.^iter./factorial(iter)
KSSV
KSSV on 7 Dec 2020
What is the problem you are facing? What is the input you trying. Ask your question clearly.
I fixed the error, but this error will appear again
--------------------------------------------------------
Not enough input arguments.
Error in EX3 (line 16)
if ea<=es || iter >= maxit,break,end
You appear to be running your function but passing in either only one parameter, or else two parameters. You need to pass in three parameters to your function.
%true
if ea<=es || iter >= maxit
break;
end
Use a semicolon ; not a comma ,
comma is acceptable syntax there.
if ea<=es || iter >= maxit,break,end
means the equivalent of
execute the if statement test portion, and display the result. However, an if test does not have any result (and in particular, the result of it is not the value of ea<=es || iter >= maxit so nothing will be displayed
Then, under the condition that the test succeeded, execute the break statement, and display the result. However, a break does not have a result, so nothing will be displayed
Comma acts as a statement separator that does not supress the result of compution... but in this case there is no result of computation to display so it does not matter that you used comma instead of semi-colon.
Something that would be perfectly valid would be
if iter > 1, iter, end
and that would be the same as
if iter > 1
iter
end
Well, the same except for some rules about exactly when interrupts can occur.
VBBV
VBBV on 8 Dec 2020
Ok. Is it same when there is keyword after comma ? In the e.g. you cited its a variable iter which changes values in the program
%if true
if ea<=es || iter >= maxit,break,end
Give a value to maxit variable before it enter the while loop

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 7 Dec 2020
Edited: Walter Roberson on 7 Dec 2020
xr = [ ];
That is empty.
xrold=xr;
so that is empty.
xr= xr-x^iter/factorial(iter); %<--------------------------here?
empty minus anything would be empty.
You are passing in x, but if it is not a scalar, you need
xr = xr - x.^iter ./ factorial(iter);

Categories

Find more on Functions in Help Center and File Exchange

Products

Release

R2019b

Tags

Asked:

on 7 Dec 2020

Commented:

on 8 Dec 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!