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

查看:35
本文介绍了为什么我在 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:UsersGTAVDocumentsMATLABCritPt.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天全站免登陆