How to calculate the equation with letter and variable

조회 수: 118 (최근 30일)
HONG CHENG
HONG CHENG 2017년 5월 4일
편집: Stephen23 2017년 10월 17일
Dear everyone,
I want to calculate an equation with letter and variable.
Now I can get the variable value presented with the letters I used
but I don't know how to change the letters as real numbers.
this is the example
%declear syms
syms x0 y0 x1 y1 x2 y2 a b p q d positive;
[x0 y0] = solve('(x-p)^2+(y-q)^2=d^2','(p-a)*(y-b)=(x-a)*(q-b)')
then I can get the result
but the problem is how to enter the real number value of letters like
a = 1;
b = 2;
p = 3;
q = 4;
d = 5;
then I can get the numerical value of x, y ???
  댓글 수: 1
HONG CHENG
HONG CHENG 2017년 5월 4일
편집: HONG CHENG 2017년 5월 4일
Maybe we can use the subs() function to do this
f=subs(solve('(x-p)^2+(2-q)^2=d^2') ,{a,b,p,q,d},{1,2,3,4,5})
but here we just can use one equation.
if anyone have other or better method, please show your idea

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2017년 5월 4일
The substitution will occur automatically. The problem is that you must put ‘x’, ‘x0’ and ‘y0’ in the equations you want to solve for them.
This will do the substitutions:
syms x0 y0 x1 y1 x2 y2 a b p q d positive
a = 1;
b = 2;
p = 3;
q = 4;
d = 5;
Eq1 = (x-p)^2+(y-q)^2==d^2;
Eq2 = (p-a)*(y-b)==(x-a)*(q-b);
[x0 y0] = solve(Eq1, Eq2);
  댓글 수: 9
Walter Roberson
Walter Roberson 2017년 5월 7일
HONG CHENG comments on Star Strider's Answer:
kind and big god in MATLAB
Karan Gill
Karan Gill 2017년 5월 9일
See my answer below for why you only get one solution.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Karan Gill
Karan Gill 2017년 5월 9일
편집: Stephen23 2017년 10월 17일
Use subs to substitute values, as shown below. You only get one solution because in the other solution, "x" is negative, which is not allowed due to the assumption that it is positive.
BUT if don't substitute values before solving, then you get two solutions because the second solution can be positive under certain conditions. "solve" also issues a warning stating that conditions apply to the solutions. If you use the "ReturnConditions" option, then you get these conditions. Applying these conditions will let you find correct values. See the doc: https://www.mathworks.com/help/symbolic/solve-an-algebraic-equation.html.
syms x y a b p q d positive
eqn1 = (x-p)^2+(y-q)^2 == d^2;
eqn2 = (p-a)*(y-b) == (x-a)*(q-b);
vars = [a b p q d];
vals = sym([1 2 3 4 5]);
eqn1 = subs(eqn1,vars,vals);
eqn2 = subs(eqn2,vars,vals);
[xSol ySol] = solve(eqn1, eqn2)
xSol =
(5*2^(1/2))/2 + 3
ySol =
(5*2^(1/2))/2 + 4
Lastly, do not redeclare symbolic variables as doubles because you are overwriting them. So don't do this.
syms a
a = 1
Just do
a = 1
Or use "subs" to substitute for "a" in an expression
f = a^2
subs(f,a,2)
Karan (Symbolic doc)
  댓글 수: 3
Karan Gill
Karan Gill 2017년 5월 9일
Glad to hear that! If my answer was helpful, could you accept it so that others can find the reason.
HONG CHENG
HONG CHENG 2017년 5월 10일
편집: HONG CHENG 2017년 5월 10일
Sorry, I will reopen another question. Can you answer in this link - a new question ?
Because I think Another Answer does a lot help for me too.
I will accept the answer in this new link. Thanks a lot !!!1

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!