Error in solve (line 226)

26 visualizaciones (últimos 30 días)
Huda Alzaki
Huda Alzaki el 27 de Sept. de 2019
Comentada: Walter Roberson el 7 de Mzo. de 2021
I always get an error while trying to solve even simple equation. I have this code:
clc, clear
syms N
N= 10;
y = 1/N^2;
answer = solve (y,N)
and I got this error:
Error using solve (line 266)
Specify the variable to solve for.
Error in Untitled5 (line 5)
answer = solve (y,N)
Please help
  3 comentarios
venkat reddy
venkat reddy el 7 de Mzo. de 2021
how to solve this
please help me
Walter Roberson
Walter Roberson el 7 de Mzo. de 2021
use the debugger
dbstop if error
and run the code. When it stops, show us the line of code that it is saying is the problem, and the error message, and the size() and class() of all the variables mentioned on the line.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 28 de Sept. de 2019
subs(y, N, 10)
without having assigned 10 to the symbolic variable N.
If you have gone ahead and assigned a numeric value to what was previously a symbolic variable (not recommended!) then you can use either
subs(y, sym('N'), N)
or
subs(y)
The first of those would substitute only for the variable N, but the second would substitute for any variable in y that has been given a value.

Más respuestas (4)

Katarina Vuckovic
Katarina Vuckovic el 27 de Sept. de 2019
You are setting the "unknown" value to be N in the first line and then you are setting N =10 in teh second line. If you want to solve for y, you should set your "unknown" to be y.
Options to fix the code:
1) If you just want to find the value of y, you don't need to use the "solve" function. Just say
y = 1/N^2
2) If you want to use the "solve" function, you can do something like y = 1/N^2 therefore N = 1/sqrt(y) so now you have 10=1/sqrt(y):
syms y
y = 10
eqn = 1/sqrt(y) == 10;
S = solve(eqn,y)
  1 comentario
Walter Roberson
Walter Roberson el 28 de Sept. de 2019
syms y
y = 10
The y = 10 there overrides the syms y and would give the same problem as the original poster. That y = 10 should be removed.

Iniciar sesión para comentar.


Steven Lord
Steven Lord el 27 de Sept. de 2019
In your code, you first defined N to be a symbolic variable using syms. However, on the very next line you redefined it to be the number 10. This makes y (which you compute using N) a number rather than a symbolic expression. Since the number you pass into solve doesn't contain a symbolic variable, solve doesn't know what you want it to do.
I'm not completely sure what problem you're trying to solve, but leaving N as a symbolic variable (eliminating the line that redefines it to be 10) before calling solve then using subs to substitute a value back for N after solving may do what you want. If it doesn't, please explain in more detail your ultimate goal and we may be able to offer some suggestions for how to achieve that goal.

Huda Alzaki
Huda Alzaki el 28 de Sept. de 2019
Actully I have this expression (y = 1/N^2) and I want to get the value of y for N = 10. Sometimes, i have long algebric expressions and I want to get the value of y for multible values of N.

Katarina Vuckovic
Katarina Vuckovic el 30 de Sept. de 2019
Editada: Katarina Vuckovic el 30 de Sept. de 2019
N = 10 -> here is where you change the value of N
y = 1/(N*N)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by