Error in solve (line 226)

12 次查看(过去 30 天)
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 个评论
venkat reddy
venkat reddy 2021-3-7
how to solve this
please help me
Walter Roberson
Walter Roberson 2021-3-7
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.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2019-9-28
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.

更多回答(4 个)

Katarina Vuckovic
Katarina Vuckovic 2019-9-27
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 个评论
Walter Roberson
Walter Roberson 2019-9-28
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.

请先登录,再进行评论。


Steven Lord
Steven Lord 2019-9-27
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 2019-9-28
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 2019-9-30
编辑:Katarina Vuckovic 2019-9-30
N = 10 -> here is where you change the value of N
y = 1/(N*N)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by