What is wrong with the function code when it can work perfectly without function code

Hi There!
I am having trouble on my code where I made the code in function [input] = f(output) doesn't give me the expected answer but a normal script do.
function [k,p,err,P] = fixpt(g,po,tol,maxi)
% g is the function input as function handle
% po is the first guess
% tol is the tolerance
% maxi is the maximum iteration
% K is the number of iteration
% p is the sequence {pn}
% error is the absolute error
P(1) = po
for k = 2:maxi
P(k) = g(P(k-1));
err = abs(P(k) - P(k-1));
relerr = (err/abs(P(k)));
p = P(k);
if abs(err)<tol || abs(relerr)<tol
return
end
end
if k == maxi
fprintf('Iteration exceed maximum.')
end
end
If I do run it:
fixpt(@(x) sin(x)-1,-1,0.001,100)
It doesn't give me the the answer I expected whereas:
g = @(x) sin(x)-1
maxi = 100
tol = 0.00001
po = -1
P(1) = po
for k = 2:maxi
P(k) = g(P(k-1));
err = abs(P(k) - P(k-1));
relerr = (err/abs(P(k)));
p = P(k);
if abs(err)<tol || abs(relerr)<tol
return
end
end
if k == maxi
fprintf('Iteration exceed maximum.')
end
This does.
Im using R2021a. Thank you.

 採用された回答

Torsten
Torsten 2022 年 10 月 6 日
編集済み: Torsten 2022 年 10 月 6 日
Works for me.
[k,p,err,P] = fixpt(@(x) sin(x)-1,-1,0.00001,100)
k = 12
p = -1.9346
err = 1.0380e-05
P = 1×12
-1.0000 -1.8415 -1.9636 -1.9238 -1.9383 -1.9332 -1.9350 -1.9344 -1.9346 -1.9345 -1.9346 -1.9346
function [k,p,err,P] = fixpt(g,po,tol,maxi)
% g is the function input as function handle
% po is the first guess
% tol is the tolerance
% maxi is the maximum iteration
% K is the number of iteration
% p is the sequence {pn}
% error is the absolute error
P(1) = po;
for k = 2:maxi
P(k) = g(P(k-1));
err = abs(P(k) - P(k-1));
relerr = (err/abs(P(k)));
p = P(k);
if abs(err)<tol || abs(relerr)<tol
return
end
end
if k == maxi
fprintf('Iteration exceed maximum.')
end
end

6 件のコメント

Kenichi
Kenichi 2022 年 10 月 6 日
I don't get it, when i run it on my MATLAB it stopped on the second iteration
Kenichi
Kenichi 2022 年 10 月 6 日
It gives this error message:
Output argument "p" (and maybe others) not assigned during call to "fixpt".
Torsten
Torsten 2022 年 10 月 6 日
Maybe you used a different value for "tol" in both cases ?
Kenichi
Kenichi 2022 年 10 月 6 日
I'm not sure if that's the case, since different value of "tol" would still give the array of values..
Kenichi
Kenichi 2022 年 10 月 6 日
It seems like my indentation is wrong, I have seen your code and fixed it. Thank you!
Torsten
Torsten 2022 年 10 月 6 日
Maybe you should restart MATLAB.

サインインしてコメントする。

その他の回答 (1 件)

Davide Masiello
Davide Masiello 2022 年 10 月 6 日
編集済み: Davide Masiello 2022 年 10 月 6 日
The value of for tol differs of two orders of magnitude in the examples you gave (i.e., you used 0.001 in the function call, 0.00001 in the functionless script).
[k,p,err,P] = fixpt(@(x) sin(x)-1,-1,0.00001,100)
P = -1
k = 12
p = -1.9346
err = 1.0380e-05
P = 1×12
-1.0000 -1.8415 -1.9636 -1.9238 -1.9383 -1.9332 -1.9350 -1.9344 -1.9346 -1.9345 -1.9346 -1.9346
function [k,p,err,P] = fixpt(g,po,tol,maxi)
% g is the function input as function handle
% po is the first guess
% tol is the tolerance
% maxi is the maximum iteration
% K is the number of iteration
% p is the sequence {pn}
% error is the absolute error
P(1) = po
for k = 2:maxi
P(k) = g(P(k-1));
err = abs(P(k) - P(k-1));
relerr = (err/abs(P(k)));
p = P(k);
if abs(err)<tol || abs(relerr)<tol
return
end
end
if k == maxi
fprintf('Iteration exceed maximum.')
end
end
Is this correct?

3 件のコメント

Kenichi
Kenichi 2022 年 10 月 6 日
No, what I expected is a list that contains the iterations. In this code, when I it in the form of function, i can't get those values.
Davide Masiello
Davide Masiello 2022 年 10 月 6 日
If you copy-paste the code above in a new script, does it work?
Kenichi
Kenichi 2022 年 10 月 6 日
Yeah it does work, it seems like my indentation is the problem. Sorry that I could only choose one answer. Anyways, Thank you and have a great day.

サインインしてコメントする。

カテゴリ

製品

リリース

R2021a

質問済み:

2022 年 10 月 6 日

コメント済み:

2022 年 10 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by