Got Questions? Get Answers.
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:
Solve set of nonlinear equations

Subject: Solve set of nonlinear equations

From: Els

Date: 30 May, 2010 07:19:05

Message: 1 of 15

I try to solve a couple of nonlinear equations with the following function
--------------------
function F = SolveNG(x,cx,cy,cz,ax,ay,az,P4,P5,CP)
n = cross(P4-CP',P4-P5);
n = n(:).'/norm(n)
D1 = -n(1,1)*P4(1,1) - n(1,2)*P4(1,2) - n(1,3)*P4(1,3)

P1=P4(1,1);
P2=P4(1,2);
P3=P4(1,3);
N1=n(1,1)
N2=n(1,2)
N3=n(1,3)
F = [N1*x(1) + N2*x(2) + N3*x(3) + D1;
    ((x(1)-cx)/ax)^2 + ((x(2)-cy)/ay)^2 + ((x(3)-cz)/az)^2 -1;
    ((2*(x(1)-cx))/(ax^2))- x(4);
    ((2*(x(2)-cy))/(ay^2))- x(5);
    ((2*(x(3)-cz))/(az^2))- x(6);
    x(4)*x(1) + x(5)*x(2) + x(6)*x(3)+ x(7);
    x(4)*P1 + x(5)*P2 + x(6)*P3+ x(7)];
end
-------------------


In the main function I use this code to call my function
-------------------
x0 = [-80; -40; -40;-30;-30;-30;-30]; % Make a starting guess at the solution
        options=optimset('Display','iter'); % Option to display output
        fsolve(@SolveNG, x0, options, cx,cy,cz,ax,ay,az,P4,P5,CP)
------------------

But I get an error which I do not understand:
??? Error using ==> vertcat
CAT arguments dimensions are not consistent.

Error in ==> SolveNG at 12
F = [N1*x(1) + N2*x(2) + N3*x(3) + D1;

Error in ==> fsolve at 253
            fuser = feval(funfcn{3},x,varargin{:});

Error in ==> TestSimpeSphere at 726
        fsolve(@SolveNG, x0, options, cx,cy,cz,ax,ay,az,P4,P5,CP)

Caused by:
    Failure in initial user-supplied objective function evaluation.
    FSOLVE cannot continue.



How can I solve this??

Subject: Solve set of nonlinear equations

From: Bruno Luong

Date: 30 May, 2010 08:22:05

Message: 2 of 15

>
> How can I solve this??

Set a breakpoint in your function and check the size of the variables when it's invoked.

Bruno

Subject: Solve set of nonlinear equations

From: Els

Date: 30 May, 2010 09:29:06

Message: 3 of 15

x is a 7x1 sized variable, which should be correct, because there are 7 equations as well.
Still the error keeps coming when the F= line comes.

Subject: Solve set of nonlinear equations

From: Els

Date: 30 May, 2010 10:15:24

Message: 4 of 15

Bruno, maybe a quick look to my constants can help you determine where the problem is.

% cx = -27.6359;
% cy = 15.4697;
% cz = -11.3979;
% ax = 21.1604;
% ay = 21.1604;
% az = 21.1604;
% P4 = [-35.3578 33.0654 10.1122];
% P5 = [ -60.2000 -33.1000 -46.8000];
% CP = [-47.8215;11.6973;-6.2917];

Is it possible that the error comes from a wrong chosen x0?

Subject: Solve set of nonlinear equations

From: Bruno Luong

Date: 30 May, 2010 10:18:05

Message: 5 of 15

"Els " <y.e.t.reeuwijk@student.utwente.nl> wrote in message <httb52$mgd$1@fred.mathworks.com>...
> x is a 7x1 sized variable,

Fine, but I wrote "variables" (plurial).

Bruno

Subject: Solve set of nonlinear equations

From: Els

Date: 30 May, 2010 10:22:05

Message: 6 of 15

And the x is 7x1 sized;

x =

   -51
    -8
   -35
     0
     0
     0
     0

Subject: Solve set of nonlinear equations

From: Bruno Luong

Date: 30 May, 2010 10:26:06

Message: 7 of 15

"Els " <y.e.t.reeuwijk@student.utwente.nl> wrote in message <httdrs$dth$1@fred.mathworks.com>...
>
> Is it possible that the error comes from a wrong chosen x0?

Again Els, please use the debugger to chase the error. That is what it's designed for.

Bruno

Subject: Solve set of nonlinear equations

From: Els

Date: 30 May, 2010 10:33:05

Message: 8 of 15

> Again Els, please use the debugger to chase the error. That is what it's designed for.
>
> Bruno

I'm sorry. But how can i debug when I call this function from another function with fsolve?

Subject: Solve set of nonlinear equations

From: Bruno Luong

Date: 30 May, 2010 10:44:08

Message: 9 of 15

"Els " <y.e.t.reeuwijk@student.utwente.nl> wrote in message <httet1$iv9$1@fred.mathworks.com>...
> > Again Els, please use the debugger to chase the error. That is what it's designed for.
> >
> > Bruno
>
> I'm sorry. But how can i debug when I call this function from another function with fsolve?

Set a *breakpoint*: in the m-file editor, click on the left column next to the line number, and until you see a red dot appears, or use F12 key.

Bruno

Subject: Solve set of nonlinear equations

From: Els

Date: 30 May, 2010 10:49:04

Message: 10 of 15

Changed all the x's in XX's, and I gave in the XX
>> XX =[-51;-8;-35;0;0;0;0]
>> SolveNG(XX)

Then I debugged the function SolveNG, and I get the following error

??? Error using ==> vertcat
CAT arguments dimensions are not consistent.

Error in ==> SolveNG at 27
F = [((XX(1)-cx)/ax)^2 + ((XX(2)-cy)/ay)^2 + ((XX(3)-cz)/az)^2 -1;
 
-------------
function F = SolveNG(XX)
% F = SolveNG(XX,cx,cy,cz,ax,ay,az,P4,P5,CP)
% XX =[-51;-8;-35;0;0;0;0]
cx = -27.6359;
cy = 15.4697;
cz = -11.3979;
ax = 21.1604;
ay = 21.1604;
az = 21.1604;
P4 = [-35.3578 33.0654 10.1122];
P5 = [ -60.2000 -33.1000 -46.8000];
CP = [-47.8215;11.6973;-6.2917];

n = cross(P4-CP',P4-P5);
n = n(:).'/norm(n)
D1 = -n(1,1)*P4(1,1) - n(1,2)*P4(1,2) - n(1,3)*P4(1,3)

P1=P4(1,1);
P2=P4(1,2);
P3=P4(1,3);
N1=n(1,1)
N2=n(1,2)
N3=n(1,3)

F = [((XX(1)-cx)/ax)^2 + ((XX(2)-cy)/ay)^2 + ((XX(3)-cz)/az)^2 -1;
    ((2*(XX(1)-cx))/(ax^2))- XX(4);
    ((2*(XX(2)-cy))/(ay^2))- XX(5);
    ((2*(XX(3)-cz))/(az^2))- XX(6);
    XX(4)*XX(1) + XX(5)*XX(2) + XX(6)*XX(3)+ XX(7);
    XX(4)*P1 + XX(5)*P2 + XX(6)*P3+ XX(7);
    N1*XX(1) + N2*XX(2) + N3*XX(3) + D1];
end
--------------------

Subject: Solve set of nonlinear equations

From: Bruno Luong

Date: 30 May, 2010 11:05:10

Message: 11 of 15


> F = [((XX(1)-cx)/ax)^2 + ((XX(2)-cy)/ay)^2 + ((XX(3)-cz)/az)^2 -1; % <- Here the parse interpret as 2-column.

F = [ (pi-1); 0] % work
F = [ pi -1; 0] % don't

Break down the big expression in smaller one. That makes your code much more visible and easy to maintain.

Bruno

Subject: Solve set of nonlinear equations

From: Bruno Luong

Date: 30 May, 2010 11:14:06

Message: 12 of 15

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <httgp6$gv6$1@fred.mathworks.com>...

>
> Break down the big expression in smaller one. That makes your code much more visible and easy to maintain.
>

And second advise: When programming, always check individual function works before putting them together. In your case you seem never check F alone.

Bruno

Subject: Solve set of nonlinear equations

From: Els

Date: 30 May, 2010 13:07:04

Message: 13 of 15

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <httgp6$gv6$1@fred.mathworks.com>...
>
> > F = [((XX(1)-cx)/ax)^2 + ((XX(2)-cy)/ay)^2 + ((XX(3)-cz)/az)^2 -1; % <- Here the parse interpret as 2-column.

Dear Bruno,

Can you maybe explain what you mean by " Here the parse interpret as 2-column."

Thanks again.

Best wishes

Subject: Solve set of nonlinear equations

From: Bruno Luong

Date: 30 May, 2010 13:16:05

Message: 14 of 15

"Els " <y.e.t.reeuwijk@student.utwente.nl> wrote in message <httnto$hfq$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <httgp6$gv6$1@fred.mathworks.com>...
> >
> > > F = [((XX(1)-cx)/ax)^2 + ((XX(2)-cy)/ay)^2 + ((XX(3)-cz)/az)^2 -1; % <- Here the parse interpret as 2-column.
>
> Dear Bruno,
>
> Can you maybe explain what you mean by " Here the parse interpret as 2-column."

Sorry for the typo , should read "Here the parser interprets as 2-column."

Think about the examples I gave:

F = [pi -1;
      0]

F = [(pi -1);
      0]

Bruno

Subject: Solve set of nonlinear equations

From: Els

Date: 30 May, 2010 14:20:23

Message: 15 of 15

Wow Bruno, this did the trick. It solved all my 7 variables in once.
Thanks again.

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