In what I missed
Show older comments
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
VBBV
on 7 Dec 2020
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
on 7 Dec 2020
What is the problem you are facing? What is the input you trying. Ask your question clearly.
Saad Almalki
on 7 Dec 2020
Walter Roberson
on 7 Dec 2020
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.
VBBV
on 7 Dec 2020
%true
if ea<=es || iter >= maxit
break;
end
Use a semicolon ; not a comma ,
Walter Roberson
on 7 Dec 2020
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
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
VBBV
on 8 Dec 2020
%if true
if ea<=es || iter >= maxit,break,end
Give a value to maxit variable before it enter the while loop
Answers (1)
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
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!