为什么在Matlab中出现“调用期间未分配输出参数"错误 [英] Why am I getting 'output argument not assigned during call to' error in Matlab

查看:702
本文介绍了为什么在Matlab中出现“调用期间未分配输出参数"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以找到函数关键点的函数.

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]

我应该在命令窗口中使用函数CritPt定义一个名为cp的变量,该变量包含f(x,y)= x ^ 2 * y +(1-y)^ 2的临界点

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".

我尝试了许多替代方法,例如syms cp = [cp] =等,但显然我不了解某些内容.任何帮助将不胜感激

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

推荐答案

您在命令窗口中正确使用了该功能.

You're using the function properly in the command window.

问题出在函数CritPt本身:您需要为变量cr分配一个值.函数完成后,它将尝试返回您在function之后列出的任何变量的值,但是如果该变量不存在,则会出现错误.

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]

或者,您可以保留最后一行,但更改第一行,以便返回crpt:

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)

这篇关于为什么在Matlab中出现“调用期间未分配输出参数"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆