I got an error as symbolic function expected 3 inputs and received 1 in the line no 5 (g1{r}(u)=g1{r}(u)+(u{i}-u1(i))./5*chebyshevT(i-1,2*x(r)-1)). How can I fix that?
2 次查看(过去 30 天)
显示 更早的评论
m=3;
u=sym('u',[1,m]);
g1 = cell(m+1, 1);
for r=1:m-1;
g1{r}(u) = sym(0);
for i = 1 : m+1
g1{r}(u)=g1{r}(u)+(u{i}-u1(i))./(5)*chebyshevT(i-1,2*x(r)-1);
end
end
6 个评论
Walter Roberson
2020-9-20
g1{r}(u) = sym(0);
That syntax is the same as
g1{r} = symfun(sym(0), u)
which builds a symbolic function which accepts three distinct inputs and returns a value.
g1{r}(u)=g1{r}(u)+(u{i}-v(i))./(5)*chebyshevT(i-1,2*x(r)-1)
That code attempts to call g1{r} with a single input that is a vector of length 3.
To call g1{r} you need to pass in the elements of u as distinct inputs.
Using u{i}, I got u{1}=u1, u{2}=u2, u{3}=u3.
Which MATLAB release are you using? It would take me a bit of time to get a working copy of the old Maple-based MATLAB to test to be absolutely sure, but I guarantee that in any MuPAD based release of the symbolic toolbox, that what you indicate is incorrect.
m=3;
u=sym('u',[1,m]);
i = 1;
u{i}
Brace indexing is not supported for variables of this type.
Error in sym/subsref (line 907)
R_tilde = builtin('subsref',L_tilde,Idx);
{} is not valid for vectors of symbolic expressions.
It is not productive for me to keep arguing with you. Here is the code. Notice the complete lack of {} indexing for the symbolic vector.
m = 3;
x = rand(1,m-1); %replace with your actual data, which must be at least m-1 long
v = rand(1,m+1); %replace with your actual data, which must be at least m+1 long
u = sym('u',[1,m+1]);
uc = num2cell(u);
g1 = cell(m-1, 1);
for r=1:m-1
g1{r}(u) = sym(0);
for i = 1 : m+1
g1{r}(u)=g1{r}(uc{:})+(u(i)-v(i))./(5)*chebyshevT(i-1,2*x(r)-1);
end
end
I had to change the size of some of the arrays to fit your code. I took your "r=1:m-1" and "i=1:m+1" to be definite truths and repaired everything around those lines. That requires that u become a vector of length m+1 instead of length m, because you index u at i and i goes to m+1 . I had to assume that something was right about your code...
采纳的回答
Walter Roberson
2020-9-21
m = 3;
x = rand(1,m-1); %replace with your actual data, which must be at least m-1 long
v = rand(1,m+1); %replace with your actual data, which must be at least m+1 long
u = sym('u',[1,m+1]);
uc = num2cell(u);
g1 = cell(m-1, 1);
for r=1:m-1
g1{r}(u) = sym(0);
for i = 1 : m+1
g1{r}(u)=g1{r}(uc{:})+(u(i)-v(i))./(5)*chebyshevT(i-1,2*x(r)-1);
end
end
Under the assumption that the two "for" lines were coded correctly by the user; everything else was modified to fit that.
更多回答(1 个)
Surath Ghosh
2020-9-21
1 个评论
Walter Roberson
2020-9-21
I guarantee that in that release you cannot use {} indexing of symbolic vectors.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!