Why am I getting 'output argument not assigned during call to' error in Matlab

拜拜、爱过 提交于 2019-12-11 13:43:20

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/30023727/why-am-i-getting-output-argument-not-assigned-during-call-to-error-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!