how to solve equations with matlab

  • Follow


Hi guys

I want to solve this kind of equation:

a*x + b*y = A              (1)
a1*x^2 + b1*y^2 = B   (2)

Thanks in advanced

Zhong
0
Reply zhang 1/2/2010 1:17:04 AM

zhang skrev 2010-01-02 02:17:
> Hi guys
>
> I want to solve this kind of equation:
>
> a*x + b*y = A (1)
> a1*x^2 + b1*y^2 = B (2)
>
> Thanks in advanced
>
> Zhong

See for example:

http://www.mathworks.com/support/solutions/en/data/1-15NRJ/index.html
0
Reply Ove 1/2/2010 2:45:22 AM


On Jan 1, 8:17=A0pm, "zhang " <xiao...@gmail.com> wrote:
> Hi guys
>
> I want to solve this kind of equation:
>
> a*x + b*y =3D A =A0 =A0 =A0 =A0 =A0 =A0 =A0(1)
> a1*x^2 + b1*y^2 =3D B =A0 (2)

help solve
doc solve
mhelp solve

x =3D
[ (-1/2*b/(a1*b^2+b1*a^2)*(2*a1*b*A+2*(a1*b^2*B*a^2+b1*a^4*B-
b1*a^2*a1*A^2)^(1/2))+A)/a]
[ (-1/2*b/(a1*b^2+b1*a^2)*(2*a1*b*A-2*(a1*b^2*B*a^2+b1*a^4*B-
b1*a^2*a1*A^2)^(1/2))+A)/a]
y =3D
[ 1/2/(a1*b^2+b1*a^2)*(2*a1*b*A+2*(a1*b^2*B*a^2+b1*a^4*B-b1*a^2*a1*A^2)
^(1/2))]
[ 1/2/(a1*b^2+b1*a^2)*(2*a1*b*A-2*(a1*b^2*B*a^2+b1*a^4*B-b1*a^2*a1*A^2)
^(1/2))]

Hope this helps.

Greg
0
Reply Greg 1/2/2010 6:58:45 PM

Ove R Take <noone@the.moment> wrote in message <hhmc08$nm2$1@oden.abc.se>...
> zhang skrev 2010-01-02 02:17:
> > Hi guys
> >
> > I want to solve this kind of equation:
> >
> > a*x + b*y = A (1)
> > a1*x^2 + b1*y^2 = B (2)
> >
> > Thanks in advanced
> >
> > Zhong
> 
> See for example:
> 
> http://www.mathworks.com/support/solutions/en/data/1-15NRJ/index.html

Hello Zhong

I looked at your equation problem and have written a solution for you based on the links Ove sent.
I hope you can use the solution and that it is compatible with your MATLAB configuration. I wrote it under MATLAB R2009b with the Optimization Toolbox. 

To solve simultaneous equations you can use the 'solve' command from the Symbolic Math Toolbox,
the left division '\' operator (if a linear system of equations is involved) and the 'fsolve' command from the Optimization Toolbox as Ove indicated above.

As eq. 2 is a non-linear equation you need to use the 'fsolve' function rather than the left division operator.
Alternatively, the easier way is to use the 'solve' function from the Symbolic
Math Toolbox (if you have this). 

To solve equations of this form...

  a*x + b*y = A (1)
 a1*x^2 + b1*y^2 = B (2)

....with fsolve you need to rewrite them so that they equal zero.

so (1) and (2) become
  a*x + b*y - A = 0 (1a)
 a1*x^2 + b1*y^2 - B = 0 (2a)


then using a nested function approach (there are many other ways) you can
solve some system of non-linear equations:

  2*x  + 3*y   - 8  = 0 
 4*x^2 + 5*y^2 - 24 = 0 


THE solution
============
At the MATLAB command-line prompt you run the 'solvesimul' m-file function with the arguments
i.e. coefficients and constants, in the appropriate order.
NB: you need the Optimization Toolbox to use the 'fsolve' function that solvesimul.m uses.

>> b=solvesimul(2,3,4,5,8,24);
  1.0e-014 *

         0
    0.7105

>> b

b =

    1.0000
    2.0000


The code I wrote for 'solvesimul' is below. Save it to solvesimul.m' and run as above.
NB: you can use any one of the 3 fsolve formats by commenting out the other 2 you don't need in the m-file code below.

--------------------------------------------------
--------------------------------------------------
function S = solvesimul(a,b,a1,b1,A,B)
%solves a system (a pair) of non-linear equations returning a
%2 x 1 column solution vector for x and y.

%X = fsolve(F, InitialGuess, Options) is general format of the fn.
%where F is a function, InitialGuess is a 2 x 1 column vector, and 
%optimization options are optionally specified by 'optimset'
S = fsolve(@(x) nonlinear(x,a,b,a1,b1,A,B),[-5;-5],optimset('Display','off'));
%OR use:
%S = fsolve(@(x) nonlinear(x,a,b,a1,b1,A,B),[-5;-5]);
%OR use:
%S = fsolve(@(x) nonlinear(x,a,b,a1,b1,A,B),[-5;-5],optimset('Display','iter'));

%see how accurate the solution vector is by substituting the
%solution vector into the 'nonlinear' function...comment out if you like.
%The closer the result is to zero the better.
disp(nonlinear(S,a,b,a1,b1,A,B));

%nested function describes the simultaneous equations as a 1 x 2 row vector
%and returns a result (if zero or close to zero then solved)
    function F = nonlinear(x,a,b,a1,b1,A,B)
        % x and y are x(1) and x(2) respectively
        F = [ a*x(1)     + b*x(2)    - A
              a1*x(1).^2 + b1*x(2)^2 - B];

--------------------------------------------------
--------------------------------------------------

Regards

Bruce
0
Reply Bruce 1/3/2010 12:34:03 AM

3 Replies
165 Views

(page loaded in 0.064 seconds)


Reply: