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*cheby​shevT(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 个评论
Surath Ghosh
Surath Ghosh 2020-9-20
you suggested me earlier to write
syms u [1 m]
F = cell(m, 1);
for n = 1 : m
F{n}(u) = appropriate body for f_n;
end
So, I tried to implement my code in that way, but it is not working. Actually my target is to write u=[u1 u2 u3], where u1,u2,u3 are variables. Using u{i}, I got u{1}=u1, u{2}=u2, u{3}=u3.
So, I can access the variables.
Now I have changed u1 as v. still now I am getting same error msg as symbolic function expected 3 inputs and receive 1. Here using {}, I want to access g1_1(u), g1_2(u).....
where g1 = cell(m+1, 1), expecting g1 is a cell array.
why am I getting that error msg? Can I write the code in this way or not?
Please explain the error message.
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}-v(i))./(5)*chebyshevT(i-1,2*x(r)-1);
end
end
Walter Roberson
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
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
Surath Ghosh 2020-9-21
Thank you so much sir for your kind reply.
I am using R2016a.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by