How to properly use nargin
7 views (last 30 days)
Show older comments
I'm trying to use nargin to set a default input for a function, should that default not be previously defined by the user. I'm setting up the nargin loop in the same format my teacher used, however, I'm getting an error saying N is undefined, which is technically the point. Irronically I ran my teachers function and it also had the same error... so I'm a little stuck at the moment.
This is my function at the moment.
functiony = estimate_pi_Leibniz_Clark_Brendan(N)
if(nargin == 0)
N = 10;
end
p = 0 ;
fork = 0:N
p = p + 4*((-1)^(k)*(1/(2*k+1)));
end
y=p;
Accepted Answer
the cyclist
on 25 Apr 2021
Edited:the cyclist
on 25 Apr 2021
当你三ed to call it like this:
y = estimate_pi_Leibniz_Clark_Brendan(N)
the problem is that in the
calling
workspace, the variable N does not exist, which is why you get that error. (The error is from the calling workspace, not from your function.)
Instead you could have called it as
y = estimate_pi_Leibniz_Clark_Brendan()
which would trigger your
nargin
statement.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
开始狩猎!