Reputation: 13
I have a function that Finds the Critical Points of a function.
function [ cr ] = CritPt(f, var1, var2)
f = sym(f);
fx = diff(f,var1);
fy = diff(f,var2);
[xcr,ycr] = solve(fx,fy);
crpt = [xcr,ycr]
I am supposed to use the function CritPt in the Command Window to define a variable called cp which contains the critical points of f(x,y)=x^2*y+(1-y)^2
When I attempt this I get:
>> cp=CritPt('x^2*y+(1-y)^2','x','y')
crpt =
[ 0, 1]
[ 2^(1/2), 0]
[ -2^(1/2), 0]
Error in CritPt (line 2)
f = sym(f);
Output argument "cr" (and maybe others) not assigned
during call to
"C:\Users\GTAV\Documents\MATLAB\CritPt.m>CritPt".
I have tried many alternatives like syms cp= [cp] = etc etc but there is clearly something I am not understanding. Any help would be greatly appreciated
Upvotes: 1
Views: 7878
Reputation: 4396
You're using the function properly in the command window.
The problem is in the function CritPt
itself: You need to assign a value to the variable cr
. When the function completes, it attempts to return the value of whatever variable you have listed after function
, but if that variable is not present you get an error.
If you want to return the value of the variable on the last line, then change your last line to
cr = [xcr,ycr]
Alternatively, you can leave the last line as it is but change the first line so you return crpt
:
function [ crpt ] = CritPt(f, var1, var2)
Upvotes: 1