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

问题描述 投票:1回答:1

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

    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);

类似的代码在我尝试使用较小的函数的变量x和y时起作用。但是,当我尝试此代码时,我得到:

    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);
matlab syntax-error symbolic-math
1个回答
0
投票

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;
© www.soinside.com 2019 - 2024. All rights reserved.