Solving Equation

6 visualizzazioni (ultimi 30 giorni)
Mark
Mark il 11 Feb 2011
I'm trying to solve a equation, and I'm having some issues...
syms Xell Xelr Xerl Xerr s q; s = solve('(Xell-Xelr)/(Xerl-Xerr) = -(((s-1)*(s-(1+2/q)))/((s+1)*(s+(1+2/q))))', s);
How do i use the results and enter values in for the syms (i.e. Xell Xelr Xerl Xerr q) to solve for s?
Thanks! Mark
  1 Commento
Oleg Komarov
Oleg Komarov il 11 Feb 2011
Format the code please.

Accedi per commentare.

Risposta accettata

Oleg Komarov
Oleg Komarov il 11 Feb 2011
Not sure the best method, convert to function handles:
syms Xell Xelr Xerl Xerr s q;
s = solve('(Xell-Xelr)/(Xerl-Xerr) = -(((s-1)*(s-(1+2/q)))/((s+1)*(s+(1+2/q))))', s)
s1 = str2func(['@(Xell, Xelr, Xerl, Xerr, q)' char(s(1))]);
s2 = str2func(['@(Xell, Xelr, Xerl, Xerr, q)' char(s(2))]);
% Evaluate at the desired values
s1(1,1,3,1,2)
s2(1,1,4,1,2)
Oleg

Più risposte (3)

Mark
Mark il 11 Feb 2011
Thanks Oleg, but I hit a snag...
If you solve the following code: syms Xell Xelr Xerl Xerr s q; s = solve('(Xell-Xelr)/(Xerl-Xerr) = -(((s-1)*(s-(1+2/q)))/((s+1)*(s+(1+2/q))))', s)
you get this: (1/(2*(Q*Xell - Q*Xelr + Q*Xerl - Q*Xerr)))*(2*Xell - 2*Q*Xell - 2*Xelr + 2*Q*Xelr - 2*Xerl + 2*Q*Xerl + 2*Xerr - 2*Q*Xerr - sqrt(-4*(Q*Xell - Q*Xelr + Q*Xerl - Q*Xerr)*(-2*Xell + Q*Xell + 2*Xelr - Q*Xelr - 2*Xerl + Q*Xerl + 2*Xerr - Q*Xerr) + (-2*Xell + 2*Q*Xell + 2*Xelr - 2*Q*Xelr + 2*Xerl - 2*Q*Xerl - 2*Xerr + 2*Q*Xerr)^2));
When I run your code, I get the following: (Xelr-Xell+Xerl-Xerr-Q*Xell+Q*Xelr+Q*Xerl-Q*Xerr+(4*Q^2*Xell*Xerr-4*Q^2*Xell*Xerl+4*Q^2*Xelr*Xerl-4*Q^2*Xelr*Xerr-8*Q*Xell*Xerl+8*Q*Xell*Xerr+8*Q*Xelr*Xerl-8*Q*Xelr*Xerr+Xell^2-2*Xell*Xelr-2*Xell*Xerl+2*Xell*Xerr+Xelr^2+2*Xelr*Xerl-2*Xelr*Xerr+Xerl^2-2*Xerl*Xerr+Xerr^2)^(1/2))/(Q*Xell-Q*Xelr+Q*Xerl-Q*Xerr)
I'm get a real number for my equation, but an imaginary for yours.... Any suggestions?
Thanks for the help!
  1 Commento
Oleg Komarov
Oleg Komarov il 11 Feb 2011
I'm running your part of the code first and then my part of the code and don't get the difference you say.
Clear all and run again the script, which I'll complete with your part.

Accedi per commentare.


Mark
Mark il 11 Feb 2011
Nope, I get the same issue. If I run the following code, I get the below code:
s = solve('(Xell-Xelr)/(Xerl-Xerr) = -(((s-1)*(s-(1+2/q)))/((s+1)*(s+(1+2/q))))', s)
sneg = (1/(2*(Q*Xell - Q*Xelr + Q*Xerl - Q*Xerr)))*(2*Xell - 2*Q*Xell - 2*Xelr + 2*Q*Xelr - 2*Xerl + 2*Q*Xerl + 2*Xerr - 2*Q*Xerr - sqrt(-4*(Q*Xell - Q*Xelr + Q*Xerl - Q*Xerr)*(-2*Xell + Q*Xell + 2*Xelr - Q*Xelr - 2*Xerl + Q*Xerl + 2*Xerr - Q*Xerr) + (-2*Xell + 2*Q*Xell + 2*Xelr - 2*Q*Xelr + 2*Xerl - 2*Q*Xerl - 2*Xerr + 2*Q*Xerr)^2));
spos = (1/(2*(Q*Xell - Q*Xelr + Q*Xerl - Q*Xerr)))*(2*Xell - 2*Q*Xell - 2*Xelr + 2*Q*Xelr - 2*Xerl + 2*Q*Xerl + 2*Xerr - 2*Q*Xerr + sqrt(-4*(Q*Xell - Q*Xelr + Q*Xerl - Q*Xerr)*(-2*Xell + Q*Xell + 2*Xelr - Q*Xelr - 2*Xerl + Q*Xerl + 2*Xerr - Q*Xerr) + (-2*Xell + 2*Q*Xell + 2*Xelr - 2*Q*Xelr + 2*Xerl - 2*Q*Xerl - 2*Xerr + 2*Q*Xerr)^2));
I'm getting the following for your s1 and s2
(Xelr-Xell+Xerl-Xerr-Q*Xell+Q*Xelr+Q*Xerl-Q*Xerr+(4*Q^2*Xell*Xerr-4*Q^2*Xell*Xerl+4*Q^2*Xelr*Xerl-4*Q^2*Xelr*Xerr-8*Q*Xell*Xerl+8*Q*Xell*Xerr+8*Q*Xelr*Xerl-8*Q*Xelr*Xerr+Xell^2-2*Xell*Xelr-2*Xell*Xerl+2*Xell*Xerr+Xelr^2+2*Xelr*Xerl-2*Xelr*Xerr+Xerl^2-2*Xerl*Xerr+Xerr^2)^(1/2))/(Q*Xell-Q*Xelr+Q*Xerl-Q*Xerr)
-(Xell-Xelr-Xerl+Xerr+Q*Xell-Q*Xelr-Q*Xerl+Q*Xerr+(4*Q^2*Xell*Xerr-4*Q^2*Xell*Xerl+4*Q^2*Xelr*Xerl-4*Q^2*Xelr*Xerr-8*Q*Xell*Xerl+8*Q*Xell*Xerr+8*Q*Xelr*Xerl-8*Q*Xelr*Xerr+Xell^2-2*Xell*Xelr-2*Xell*Xerl+2*Xell*Xerr+Xelr^2+2*Xelr*Xerl-2*Xelr*Xerr+Xerl^2-2*Xerl*Xerr+Xerr^2)^(1/2))/(Q*Xell-Q*Xelr+Q*Xerl-Q*Xerr)
Different then what I get using the solver.
  1 Commento
Oleg Komarov
Oleg Komarov il 11 Feb 2011
How can you get sneg = ... if you're solving s = solve...? Where does sneg come from? (btw I corrected again my original post)

Accedi per commentare.


Walter Roberson
Walter Roberson il 11 Feb 2011
You are going to get two solutions returned. You need to take that in to account. You are effectively solving a quadratic, so two solutions.
for K=1:length(s); funcs{K} = matlabFunction(s(K)); end
Now funcs{K} will be a function handle that you can pass the values in to and get solutions for s. Please see the matlabFunction documentation for information as to what parameter order the generated functions will expect.

Categorie

Scopri di più su Mathematics in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by