Error in solve (line 226)

28 ビュー (過去 30 日間)
Huda Alzaki
Huda Alzaki 2019 年 9 月 27 日
コメント済み: Walter Roberson 2021 年 3 月 7 日
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 Center および File ExchangeLinear Algebra についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by