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:
string 2 number

Subject: string 2 number

From: Ender

Date: 1 Apr, 2010 03:07:02

Message: 1 of 9

I am trying to get MATLAB to take derivatives a matrix (Jacobian basically) then after I input the value of the variables into the matrix calculate the result in the matrix. The problem is even though I input the value of the variables, MATLAb does not calculate the equations instead the program just stores the matrix with my equations with the variables inside instead. Here is my code:

% Initial approximation
XO = [0.1;0.1;-0.1];

 % First equation
                f1 = 3*x_1-cos(x_2*x_3)-1/2;
              
            % Second equation
                f2 = (x_1)^2-81*(x_2+0.1)^2+sin(x_3)+1.06;
    
            % Third equation
                f3 = exp(-x_1*x_2)+20*x_3+(10*pi-3)/3;
    
        
            % Calculate the Jacobian
                % derivative of f1 with respect to x_1
                    f1_p_x_1 = diff(f1,x_1);
        
                % derivative of f1 with respect to x_1
                    f1_p_x_2 = diff(f1,x_2);
        
                % derivative of f1 with respect to x_1
                    f1_p_x_3 = diff(f1,x_3);
           
                
                % derivative of f1 with respect to x_1
                    f2_p_x_1 = diff(f2,x_1);
        
                % derivative of f1 with respect to x_1
                    f2_p_x_2 = diff(f2,x_2);
        
                % derivative of f1 with respect to x_1
                    f2_p_x_3 = diff(f2,x_3);
        
                
                % derivative of f1 with respect to x_1
                    f3_p_x_1 = diff(f3,x_1);
        
                % derivative of f1 with respect to x_1
                    f3_p_x_2 = diff(f3,x_2);
        
                % derivative of f1 with respect to x_1
                    f3_p_x_3 = diff(f3,x_3);
           
                     x_1 = XO(1,1);
                     x_2 = XO(2,1);
                     x_3 = XO(3,1);
    
                A = [f1_p_x_1,f1_p_x_2,f1_p_x_3];
                B = [f2_p_x_1,f2_p_x_2,f2_p_x_3];
                C = [f3_p_x_1,f3_p_x_2,f3_p_x_3];
                J = [A;B;C];
                
            % Function Matrix F(x)
                F =[f1;f2;f3];
                
            % Calculate F(x)
            
    
            % Function matrix J(XO)
                J_x =[A;B;C];
                
            % Function matrix F(XO)
                F_x =[f1;f2;f3];
     
I need the numerical result for J_x and F_x

Thanks,

Ender

Subject: string 2 number

From: Jan Simon

Date: 1 Apr, 2010 13:38:07

Message: 2 of 9

Dear Ender!

> The problem is even though I input the value of the variables, MATLAb does not calculate the equations instead the program just stores the matrix with my equations with the variables inside instead. Here is my code:
>
> % Initial approximation
> XO = [0.1;0.1;-0.1];
>
> % First equation
> f1 = 3*x_1-cos(x_2*x_3)-1/2;

It is unclear to me, what this could mean: "MATLAb does not calculate the equations instead the program just stores the matrix with my equations".

Did you define "x_1" , "x_2" and "x_3" anywhere?

Kind regards, Jan

Subject: string 2 number

From: Ender

Date: 1 Apr, 2010 15:08:06

Message: 3 of 9

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hp27jv$p2p$1@fred.mathworks.com>...
> Dear Ender!
>
> > The problem is even though I input the value of the variables, MATLAb does not calculate the equations instead the program just stores the matrix with my equations with the variables inside instead. Here is my code:
> >
> > % Initial approximation
> > XO = [0.1;0.1;-0.1];
> >
> > % First equation
> > f1 = 3*x_1-cos(x_2*x_3)-1/2;
>
> It is unclear to me, what this could mean: "MATLAb does not calculate the equations instead the program just stores the matrix with my equations".
>
> Did you define "x_1" , "x_2" and "x_3" anywhere?
>
> Kind regards, Jan

Yes,

If you look at my entire code you will see that I defined these variables
                    x_1 = XO(1,1);
                     x_2 = XO(2,1);
                     x_3 = XO(3,1);

Ender

Subject: string 2 number

From: Jan Simon

Date: 1 Apr, 2010 15:25:27

Message: 4 of 9

Dear Ender!

> > It is unclear to me, what this could mean: "MATLAb does not calculate the equations instead the program just stores the matrix with my equations".

Could you explain this sentence?
It is not the usual behaviour of Matlab not to calculate equations. So please show exactly, what you are doing, expecting and what you get.

> > Did you define "x_1" , "x_2" and "x_3" anywhere?
> Yes,
>
> If you look at my entire code you will see that I defined these variables
> x_1 = XO(1,1);
> x_2 = XO(2,1);
> x_3 = XO(3,1);
> Ender

Ok, you've defined it, but *after* you try to access it?!

Kind regards, Jan

Subject: string 2 number

From: Ender

Date: 1 Apr, 2010 15:44:20

Message: 5 of 9

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hp2dt6$ekj$1@fred.mathworks.com>...
> Dear Ender!
>
> > > It is unclear to me, what this could mean: "MATLAb does not calculate the equations instead the program just stores the matrix with my equations".
>
> Could you explain this sentence?
> It is not the usual behaviour of Matlab not to calculate equations. So please show exactly, what you are doing, expecting and what you get.
>
> > > Did you define "x_1" , "x_2" and "x_3" anywhere?
> > Yes,
> >
> > If you look at my entire code you will see that I defined these variables
> > x_1 = XO(1,1);
> > x_2 = XO(2,1);
> > x_3 = XO(3,1);
> > Ender
>
> Ok, you've defined it, but *after* you try to access it?!
>
> Kind regards, Jan

I am not sure what you mean by accessing it. I am assuming you mean when call the variable or display the solution. Whenever I ask to display the solution, MATlAb returns my solution as a string still containing my variable names. I want MATLAB to plug in the value for "x_1" and calculate the equation in the matrix.

e.g Matlab display this:

F_x =
 
                                3*x_1-cos(x_2*x_3)-1/2
                  x_1^2-81*(x_2+1/10)^2+sin(x_3)+53/50
 exp(-x_1*x_2)+20*x_3+5332248173269055/562949953421312
 
J_x =
 
[ 3, sin(x_2*x_3)*x_3, sin(x_2*x_3)*x_2]
[ 2*x_1, -162*x_2-81/5, cos(x_3)]
[ -x_2*exp(-x_1*x_2), -x_1*exp(-x_1*x_2), 20]
 
 
I want the numerical result not the equations.

--Ender--

Subject: string 2 number

From: Matt Fig

Date: 1 Apr, 2010 16:09:04

Message: 6 of 9

"Ender " <jr147@msstate.edu> wrote in message
> I am not sure what you mean by accessing it. I am assuming you mean when call the variable or display the solution. Whenever I ask to display the solution, MATlAb returns my solution as a string still containing my variable names. I want MATLAB to plug in the value for "x_1" and calculate the equation in the matrix.
>
> e.g Matlab display this:
>
> F_x =
>
> 3*x_1-cos(x_2*x_3)-1/2
> x_1^2-81*(x_2+1/10)^2+sin(x_3)+53/50
> exp(-x_1*x_2)+20*x_3+5332248173269055/562949953421312
>
> J_x =
>
> [ 3, sin(x_2*x_3)*x_3, sin(x_2*x_3)*x_2]
> [ 2*x_1, -162*x_2-81/5, cos(x_3)]
> [ -x_2*exp(-x_1*x_2), -x_1*exp(-x_1*x_2), 20]
>
>
> I want the numerical result not the equations.
>
> --Ender--



Then there is more to the story than you have given, because when I copy and paste your code, I get an error (as I, and Jan, expect).



% Initial approximation
XO = [0.1;0.1;-0.1];

                f1 = 3*x_1-cos(x_2*x_3)-1/2;
??? Undefined function or variable 'x_1'.

Undefined, because it is accessed before it is defined! So what have you left out?

Subject: string 2 number

From: Jan Simon

Date: 1 Apr, 2010 16:13:19

Message: 7 of 9

Dear Ender!


> > It is not the usual behaviour of Matlab not to calculate equations. So please show exactly, what you are doing, expecting and what you get.


> I am not sure what you mean by accessing it. I am assuming you mean when call the variable or display the solution. Whenever I ask to display the solution, MATlAb returns my solution as a string still containing my variable names. I want MATLAB to plug in the value for "x_1" and calculate the equation in the matrix.
>
> e.g Matlab display this:
>
> F_x =
> 3*x_1-cos(x_2*x_3)-1/2
> x_1^2-81*(x_2+1/10)^2+sin(x_3)+53/50
> exp(-x_1*x_2)+20*x_3+5332248173269055/562949953421312
>
> J_x =
> [ 3, sin(x_2*x_3)*x_3, sin(x_2*x_3)*x_2]
> [ 2*x_1, -162*x_2-81/5, cos(x_3)]
> [ -x_2*exp(-x_1*x_2), -x_1*exp(-x_1*x_2), 20]
>
> I want the numerical result not the equations.

Again: "Whenever I ask to display the solution, MATlAb returns my solution as a string" is not sufficient to reproduce the problem. How do you "ask" Matlab? Please show us the (relevant - the shorter the better, but do not omit important details!) commands exactly as you type them in the command window and the contents of the functions as they are defined in the M-files!
Is this the first problem you process in Matlab or did you solve other calculation numerically in Matlab before?

Please begin with a simpler problem:
  x = 5
  y = [sin(x), cos(x), 8]
Do you get a numerical reply? If yes, what are you doing differently, such that you get your non-numerical answers? Did you tell Matlab, that you want to solve x_1, etc symbolically?

Kind regards, Jan

Subject: string 2 number

From: Steven Lord

Date: 1 Apr, 2010 17:00:13

Message: 8 of 9


"Ender " <jr147@msstate.edu> wrote in message
news:hp2f0k$3tb$1@fred.mathworks.com...
> "Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message
> <hp2dt6$ekj$1@fred.mathworks.com>...
>> Dear Ender!
>>
>> > > It is unclear to me, what this could mean: "MATLAb does not calculate
>> > > the equations instead the program just stores the matrix with my
>> > > equations".
>>
>> Could you explain this sentence?
>> It is not the usual behaviour of Matlab not to calculate equations. So
>> please show exactly, what you are doing, expecting and what you get.
>>
>> > > Did you define "x_1" , "x_2" and "x_3" anywhere?
>> > Yes,
>> >
>> > If you look at my entire code you will see that I defined these
>> > variables
>> > x_1 = XO(1,1);
>> > x_2 = XO(2,1);
>> > x_3 = XO(3,1);
>> > Ender
>>
>> Ok, you've defined it, but *after* you try to access it?!
>>
>> Kind regards, Jan
>
> I am not sure what you mean by accessing it. I am assuming you mean when
> call the variable or display the solution. Whenever I ask to display the
> solution, MATlAb returns my solution as a string still containing my
> variable names. I want MATLAB to plug in the value for "x_1" and calculate
> the equation in the matrix.

The missing piece of code that is present in your code but not in your
original posting is, I believe,

    syms x_1 x_2 x_3

or something similar. If that's the case, you need to explicitly tell
MATLAB to SUBStitute the numeric values into the symbolic expression; it
doesn't perform that substitution by default.

    J_x_numeric = subs(J_x)
    F_x_numeric = subs(F_x)

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: string 2 number

From: Ender

Date: 1 Apr, 2010 17:52:06

Message: 9 of 9

"Steven Lord" <slord@mathworks.com> wrote in message <hp2je7$fsh$1@fred.mathworks.com>...
>
> "Ender " <jr147@msstate.edu> wrote in message
> news:hp2f0k$3tb$1@fred.mathworks.com...
> > "Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message
> > <hp2dt6$ekj$1@fred.mathworks.com>...
> >> Dear Ender!
> >>
> >> > > It is unclear to me, what this could mean: "MATLAb does not calculate
> >> > > the equations instead the program just stores the matrix with my
> >> > > equations".
> >>
> >> Could you explain this sentence?
> >> It is not the usual behaviour of Matlab not to calculate equations. So
> >> please show exactly, what you are doing, expecting and what you get.
> >>
> >> > > Did you define "x_1" , "x_2" and "x_3" anywhere?
> >> > Yes,
> >> >
> >> > If you look at my entire code you will see that I defined these
> >> > variables
> >> > x_1 = XO(1,1);
> >> > x_2 = XO(2,1);
> >> > x_3 = XO(3,1);
> >> > Ender
> >>
> >> Ok, you've defined it, but *after* you try to access it?!
> >>
> >> Kind regards, Jan
> >
> > I am not sure what you mean by accessing it. I am assuming you mean when
> > call the variable or display the solution. Whenever I ask to display the
> > solution, MATlAb returns my solution as a string still containing my
> > variable names. I want MATLAB to plug in the value for "x_1" and calculate
> > the equation in the matrix.
>
> The missing piece of code that is present in your code but not in your
> original posting is, I believe,
>
> syms x_1 x_2 x_3
>
> or something similar. If that's the case, you need to explicitly tell
> MATLAB to SUBStitute the numeric values into the symbolic expression; it
> doesn't perform that substitution by default.
>
> J_x_numeric = subs(J_x)
> F_x_numeric = subs(F_x)
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>


I thought I had mentioned that in my first email (e.g. syms).
Your suggestion worked.

Thanks

--Ender--

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