How to calculate the equation with letter and variable

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 comentario

HONG CHENG
HONG CHENG el 4 de Mayo de 2017
Editada: HONG CHENG el 4 de Mayo de 2017
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

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 4 de Mayo de 2017
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 comentarios

HONG CHENG
HONG CHENG el 4 de Mayo de 2017
Editada: HONG CHENG el 4 de Mayo de 2017
Dear Star Strider ,
Thanks for your answer.I think your method works well.
>> syms x0 y0 x1 y1 x2 y2 a b p q d positive
a = 1;
b = 2;
p = 3;
q = 4;
d = 5;
Eq1 = (x0-p)^2+(y0-q)^2==d^2;
Eq2 = (p-a)*(y0-b)==(x0-a)*(q-b);
[x0 y0] = solve(Eq1, Eq2);
>> x0
x0 =
(5*2^(1/2))/2 + 3
>> y0
y0 =
(5*2^(1/2))/2 + 4
and even there is a quadratic formula, we just get a solution.Very nice.
But I don't know why there is just one solution. maybe because we have two equation to caluculate?
but my old method have two solutions.
Would you like to explain more for me ? thanks in advance!
And without the real number we get this
clear
clc
%declear syms
syms x0 y0 x1 y1 x2 y2 a b p q d positive;
%%the coodinates of S(x0,y0)
% a = 1;
% b = 2;
% p = 3;
% q = 4;
% d = 5;
Eq1 = (x0-p)^2+(y0-q)^2==d^2;
Eq2 = (p-a)*(y0-b)==(x0-a)*(q-b);
[x0 y0] = solve(Eq1, Eq2);
% y0 = round(y0)
% x0 = round(x0)
x0
x0 =
-(a*q - b*p - a*(q^3/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) + b*d*(1/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2))^(1/2) + (a^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - (2*b*q^2)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) + (b^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - d*q*(1/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2))^(1/2) + (p^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - (2*a*p*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2)) + p*(q^3/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) + b*d*(1/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2))^(1/2) + (a^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - (2*b*q^2)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) + (b^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - d*q*(1/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2))^(1/2) + (p^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - (2*a*p*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2)))/(b - q)
-(a*q - b*p - a*(q^3/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - b*d*(1/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2))^(1/2) + (a^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - (2*b*q^2)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) + (b^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) + d*q*(1/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2))^(1/2) + (p^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - (2*a*p*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2)) + p*(q^3/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - b*d*(1/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2))^(1/2) + (a^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - (2*b*q^2)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) + (b^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) + d*q*(1/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2))^(1/2) + (p^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - (2*a*p*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2)))/(b - q)
y0
y0 =
q^3/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) + b*d*(1/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2))^(1/2) + (a^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - (2*b*q^2)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) + (b^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - d*q*(1/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2))^(1/2) + (p^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - (2*a*p*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2)
q^3/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - b*d*(1/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2))^(1/2) + (a^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - (2*b*q^2)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) + (b^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) + d*q*(1/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2))^(1/2) + (p^2*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2) - (2*a*p*q)/(a^2 - 2*a*p + b^2 - 2*b*q + p^2 + q^2)
My pleasure.
To get the quadratic solutions, it is necessary to solve the second equation, substitute that solution into the first equation, and then solve the first equation:
syms x0 y0 x1 y1 x2 y2 a b p q d positive
a = 1;
b = 2;
p = 3;
q = 4;
d = 5;
Eq1 = (x0-p)^2+(y0-q)^2==d^2;
Eq2 = (p-a)*(y0-b)==(x0-a)*(q-b);
x0 = solve(Eq2, x0); % First, Solve ‘Eq2’ For ‘x0’
y0 = solve(subs(Eq1), y0); % Then Substitute & Solve ‘Eq1’ To Yield The Quadratic Solutions
y0 =
4 - (5*2^(1/2))/2
(5*2^(1/2))/2 + 4
Those should be the same as the solutions from your previous method.
HONG CHENG
HONG CHENG el 4 de Mayo de 2017
Editada: HONG CHENG el 4 de Mayo de 2017
thanks for your quick reply.
with your example and interpret, I have understood a lot by the mechanism for calculate this problem.
However, as your coding shows to us, there are two y0 solutions here, and if we check out the x0 solution, it is just x0 = y0 -1
what are the reasons that the matlab chooses the solution "(5*2^(1/2))/2 + 4" not the solution " 4 - (5*2^(1/2))/2" and then get the x0 solution "(5*2^(1/2))/2 + 3"?
And in fact, if we just know the y0 solution ,we STILL have two x0 solutions
I am sorry I am not fully understanding how to get the final x0, y0 solution respectively.
I think there is just one step to get the key. Hoping you can explain more.
Thanks a lot!!!
My pleasure.
The result appears to be dependent on how you set up the problem. Once you solve for ‘y0’, you can get ‘x0’ by substituting th ‘y0’ value into:
x0 = y0 - 1;
So:
syms x0 y0 x1 y1 x2 y2 a b p q d positive
a = 1;
b = 2;
p = 3;
q = 4;
d = 5;
Eq1 = (x0-p)^2+(y0-q)^2==d^2;
Eq2 = (p-a)*(y0-b)==(x0-a)*(q-b);
x0 = solve(Eq2, x0) % First, Solve ‘Eq2’ For ‘x0’
y0 = solve(subs(Eq1), y0) % Then Substitute & Solve ‘Eq1’ To Yield The Quadratic Solutions
x0s = subs(x0)
y0 =
4 - (5*2^(1/2))/2
(5*2^(1/2))/2 + 4
x0s =
3 - (5*2^(1/2))/2
(5*2^(1/2))/2 + 3
I do not know the reason that asking for the simultaneous solution produces only one value for each, rather than two.
Consider asking MathWorks about this, and suggest that this could be a bug.
HONG CHENG
HONG CHENG el 6 de Mayo de 2017
Dear Star Strider, I reconsider this problem. In my view, this is not a bug.Maybe I do not explain clearly.
I want to calculate a point coordinates where this point V(x0,y0) is between two given point T(p,q),A(a,b).And we know that the length of line TV,d.
So I think it't OK we have two solutions. By the information that _ point V(x0,y0) is between point T(p,q) and A(a,b)_, we can choose the right solution finally.
  • Now I have draw this figure.
Your kind assistance on this are very much appreciated. And I think you help me a lot in this problem. Thanks a lot, my friends!
Star Strider
Star Strider el 6 de Mayo de 2017
As always, my pleasure!
It appears that you have resolved this, and you have a method to choose the ‘correct’ ‘(x0,y0)’.
HONG CHENG comments on Star Strider's Answer:
kind and big god in MATLAB
Karan Gill
Karan Gill el 9 de Mayo de 2017
See my answer below for why you only get one solution.

Iniciar sesión para comentar.

Más respuestas (1)

Karan Gill
Karan Gill el 9 de Mayo de 2017
Editada: Stephen23 el 17 de Oct. de 2017
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 comentarios

HONG CHENG
HONG CHENG el 9 de Mayo de 2017
dear Karan, thanks for your kind answer.
With your explanation,I know the real reason I get two values. Thanks a lot!
Karan Gill
Karan Gill el 9 de Mayo de 2017
Glad to hear that! If my answer was helpful, could you accept it so that others can find the reason.
HONG CHENG
HONG CHENG el 10 de Mayo de 2017
Editada: HONG CHENG el 10 de Mayo de 2017
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

Iniciar sesión para comentar.

Categorías

Más información sobre Weather and Atmospheric Science en Centro de ayuda y File Exchange.

Preguntada:

el 4 de Mayo de 2017

Editada:

el 17 de Oct. de 2017

Community Treasure Hunt

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

Start Hunting!