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?
3 views (last 30 days)
Show older comments
Surath Ghosh
on 19 Sep 2020
Commented: Surath Ghosh
on 21 Sep 2020
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 Comments
Walter Roberson
on 20 Sep 2020
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...
Accepted Answer
Walter Roberson
on 21 Sep 2020
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.
More Answers (1)
Surath Ghosh
on 21 Sep 2020
1 Comment
Walter Roberson
on 21 Sep 2020
I guarantee that in that release you cannot use {} indexing of symbolic vectors.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!