Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how can i solve inline system of equations using fsolve()

Subject: how can i solve inline system of equations using fsolve()

From: Tian

Date: 23 Dec, 2012 13:28:07

Message: 1 of 3

I am a programmer but am new to the syntax of matlab.
I wonder how I could fsolve a system of nonlinear functions. Here is my code:

    x = X(i-1)
    y = Y(i-1)
    a = (sqrt(3)-1)/(2*sqrt(3))
    b = (sqrt(3)+1)/(2*sqrt(3))
    c = (sqrt(3)-2)/(4*sqrt(3))
    d = (sqrt(3)+2)/(4*sqrt(3))
    
    G = @(k)('[k(:,1) - (4*(x + a*h) + y + h/2*k(:,1) + c * h * k(:,2) - 1)^(0.5); k(:,2) - (4*(x + b*h) + y + d*h*k(:,1) + h/4*k(:,2) -1)^(0.5)]')
   
    [k, fval] = fsolve(G, [0.0,0.0])


Basically, I want to use the two equations to solve for k with k1 and k2....
But I received the following error message:

Error using fsolve (line 280)
FSOLVE requires all values returned by user functions to be of
data type double.
Error in rk4 (line 26)
    [k, fval] = fsolve(G, [0.0,0.0])

I don't know what's wrong. Thx.

Subject: how can i solve inline system of equations using fsolve()

From: Nasser M. Abbasi

Date: 23 Dec, 2012 15:24:05

Message: 2 of 3

On 12/23/2012 7:28 AM, Tian wrote:
> I am a programmer but am new to the syntax of matlab.
> I wonder how I could fsolve a system of nonlinear functions. Here is my code:
>

I think fsolve wants numbers, not symbols. Try with actual numbers.

also your code posted is not complete.


> x = X(i-1)
> y = Y(i-1)
> a = (sqrt(3)-1)/(2*sqrt(3))
> b = (sqrt(3)+1)/(2*sqrt(3))
> c = (sqrt(3)-2)/(4*sqrt(3))
> d = (sqrt(3)+2)/(4*sqrt(3))
>
> G = @(k)('[k(:,1) - (4*(x + a*h) + y + h/2*k(:,1) + c * h * k(:,2) - 1)^(0.5); k(:,2) - (4*(x + b*h) + y + d*h*k(:,1) + h/4*k(:,2) -1)^(0.5)]')
>
> [k, fval] = fsolve(G, [0.0,0.0])
>
>
> Basically, I want to use the two equations to solve for k with k1 and k2....
> But I received the following error message:
>
> Error using fsolve (line 280)
> FSOLVE requires all values returned by user functions to be of
> data type double.
> Error in rk4 (line 26)
> [k, fval] = fsolve(G, [0.0,0.0])
>
> I don't know what's wrong. Thx.
>

Subject: how can i solve inline system of equations using fsolve()

From: Roger Stafford

Date: 23 Dec, 2012 19:13:08

Message: 3 of 3

"Tian " <tc28@cs.brown.edu> wrote in message <kb70t7$pgj$1@newscl01ah.mathworks.com>...
> G = @(k)('[k(:,1) - (4*(x + a*h) + y + h/2*k(:,1) + c * h * k(:,2) - 1)^(0.5); k(:,2) - (4*(x + b*h) + y + d*h*k(:,1) + h/4*k(:,2) -1)^(0.5)]')
>
> [k, fval] = fsolve(G, [0.0,0.0])
- - - - - - - - - -
  Your equations can fairly easily be reduced by hand to finding the four roots of a certain quartic equation for which you could use matlab's 'roots' function. Or you could use matlab's 'solve' to do that reducing for you. Perhaps 'solve' might even get the numerical solution for you (provided you furnish the values of x, y, and h.) That would avoid making possibly inappropriate initial estimates for 'fsolve'.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us