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?

1 visualización (últimos 30 días)
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 comentarios
Surath Ghosh
Surath Ghosh el 20 de Sept. de 2020
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 el 20 de Sept. de 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...

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 21 de Sept. de 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.

Más respuestas (1)

Surath Ghosh
Surath Ghosh el 21 de Sept. de 2020
Thank you so much sir for your kind reply.
I am using R2016a.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by