defining constants in a equation

5 visualizaciones (últimos 30 días)
samira
samira el 29 de Jun. de 2011
Hi
I want to define some equations and the solve them. How can I define constants in a matrix? By that I am able to change constants and code it.
Thanks samira
  1 comentario
Keshav Dev Singh
Keshav Dev Singh el 29 de Jun. de 2011
Can you explain your question little more.I am not understanding
if it is constant then why need to change its value?

Iniciar sesión para comentar.

Respuestas (4)

Paulo Silva
Paulo Silva el 29 de Jun. de 2011
Just guessing by the tags, if you are referring to the solve function
syms a b x %your symbolic variables
c=1; %your constant value
solve(a*x^2 + b*x + c) %solve it and get one familiar expression

Walter Roberson
Walter Roberson el 29 de Jun. de 2011
In addition to Paulo's response about subs():
Depending upon your function, you might be able to solve() the matrix symbolically, and then use matlabFunction() to create a MATLAB function that accepts numeric values of the symbols as arguments and calculates a numeric output.

samira
samira el 29 de Jun. de 2011
I mean for example I have two equations for two cirles as follow: (x-a)^2+(Y-b)^2=r^2 I want to be able to change a, b and r value in a loop and solve the equations for several circles.
I hope it is more clear now. Thanks
  1 comentario
Paulo Silva
Paulo Silva el 29 de Jun. de 2011
make all letters symbolic and use the subs function to replace the letters you want to be constant in the loop
subs(S,old,new)
Just a small remark, use a copy of the function so you can replace values and reuse the initial function for the next loops.

Iniciar sesión para comentar.


samira
samira el 29 de Jun. de 2011
Thanks Paulo and Walter
Can you help me with sth else too?
I tried this code: syms x y r1 r2 a1 a2 b1 b2; one=sym('(x-a1)^2-(y-b1)^2=r1^2'); two=sym('(x-a2)^2-(y-b2)^2=r2^2'); three=subs(one,{a1,b1,r1},{1, 1,0.5}); four=subs(two,{a2,b2,r2},{1.5, 1,0.5}); answer=solve(three,four)
These are the results for y value, can you tell me what is "i" and how can I get unique value for y values? answer.x
ans =
5/4
5/4
>> answer.y
ans =
1 + (3^(1/2)*i)/4
1 - (3^(1/2)*i)/4
  2 comentarios
Paulo Silva
Paulo Silva el 29 de Jun. de 2011
i is the imaginary unit, it's equal to sqrt(-1)
You get two values for y probably because of the quadratic equation, read this http://en.wikipedia.org/wiki/Quadratic_equation
Walter Roberson
Walter Roberson el 29 de Jun. de 2011
In that context, "i" is the square root of negative 1.
Are you sure you want (x-a1)^2 MINUS (y-b1)^2 ? That does not define a circle.
Either way, consider that for real values y, (y-b1)^2 = (abs(y-b1))^2, so your equation inherently maps two different points to the same value. solve() has no way of choosing between the two values unless you give it more information about which is the "right" value for your purpose.
It seems to me that you should probably not be asking for unique y values: two points of intersection is to be expected. Which one you want to use (if you only want one) is going to depend upon context, not upon any inherent property such as (say) which y value is larger.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by