필터 지우기
필터 지우기

Error in solve (line 226)

조회 수: 14 (최근 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 CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by