matlab - MATLAB无法存储具有多个变量的符号函数

标签 matlab syntax-error symbolic-math

我正在尝试针对x从-1到1的定积分。该函数具有变量abcdx,我将它们全部定义为syms变量。我试图将abcd保留在我的最终积分中,因为稍后我将针对优化问题针对每一个进行区分。这是我当前拥有的代码:

syms f(x);
syms a b c d;
f(x)= (exp(x)-a*(1/sqrt(2))-b*(sqrt(3/2)*x)-c((sqrt(45/8))*(x^2-(1/3)))+d((sqrt(175/8))*((x^3)-(3/5)*(x))))^2;
integral = int(f, x, [-1 1]);
disp(integral);

当我尝试使用较小的函数的变量xy时,类似的代码有效。但是,当我尝试这段代码时,我得到:

Error using sym/subsindex (line 825) Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.

Error in sym/subsref (line 870)
R_tilde = builtin('subsref',L_tilde,Idx);

Error in HW11 (line 4)
f(x)= (exp(x)-a*(1/sqrt(2))-b*(sqrt(3/2)x)-c((sqrt(45/8))(x^2-(1/3)))+d((sqrt(175/8))((x^3)-(3/5)(x))))^2;



我对MATLAB中的符号函数和syms变量还很陌生,为什么MATLAB拒绝此代码?我尝试过的类似代码是:
syms f(x);
syms y;
f(x) = (x^2) + y;
integral = int(f, x, [0 3]);
disp(integral);

最佳答案

正如commentAdam中所提到的那样,您可能忘记了在*c之后添加一个乘法运算符d,因此,当您编写c(...)d(...)时,MATLAB将它们视为数组的索引,但无法使用符号变量或表达式对数组进行索引。您需要将其更改为c*(...)d*(...)

更换:

f(x)= (exp(x)-a*(1/sqrt(2))-b*(sqrt(3/2)*x)-c((sqrt(45/8))*(x^2-(1/3)))+d((sqrt(175/8))*((x^3)-(3/5)*(x))))^2;

带有:
f(x)= (exp(x)-a*(1/sqrt(2))-b*(sqrt(3/2)*x)-c*((sqrt(45/8))*(x^2-(1/3)))+d*((sqrt(175/8))*((x^3)-(3/5)*(x))))^2;

关于matlab - MATLAB无法存储具有多个变量的符号函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59027013/

相关文章:

c# - resharper "Cannot resolve symbol"代码编译好时

r - 为mtcars数据集运行glmnet()问题

matlab - 如何在 MatLab 中求解精确微分方程?

python - How to isolate the exponent in Sympy (how to `match` large formulas in Sympy)

matlab - 通过命令执行完成后如何退出MATLAB?

c - 为什么在到达 MEX 文件的最后一行后需要这么长时间才能返回 Matlab?

git - 如何通过MATLAB脚本运行git命令?

c - 在 MEX/C 代码中访问 Matlab 类

MySQL 将值复制到另一个表

python ,同情 : How to output sympy expression as Python function?