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:
Strange experience in evaluating the value of a matrix

Subject: Strange experience in evaluating the value of a matrix

From: Timothy Siahaan

Date: 27 Jun, 2013 14:54:07

Message: 1 of 6

I have a strange experience.

The problem is as follows. I need to minimize a function of several variables. Let the function be f=f(x1,x2,..,x5). (In my case, there are 5 variables).
For that, I need to find the values of x1,...,x5 that makes the following functions:
g1=diff(f,x1);
g2=diff(f,x2);
g3=diff(f,x3);
g4=diff(f,x4);
g5=diff(f,x5);

The Newton-Raphson method is chosen for this problem, meaning that the Jacobian
Jac(i,j)=diff(g i,xj)
Should be calculated.

The strange thing appears here:
In order to do the calculation, the value of the jacobian matrix needs to be evaluated for a certain set of value of x1,x2,x3,x4,x5. After setting the values, I tried to calculate the value of the jacobian by defining a matrix JacVal as follows:
JacVal=eval(Jac).
Since my function is very long, I can understand that this process takes a very long time. However, by changing the approach of calculation, things become very different. If the matrix JacVal is defined component-wise as follows:
for a=1:5
  for b=1:5
   JacVal(a,b)=eval(Jac(a,b));
 end
end,
the result comes out much quicker.

I do not understand why it happens nor know whether the last approach is correct or not.

Any explanation/help is appreciated.

Best Regards,

Timothy

Subject: Strange experience in evaluating the value of a matrix

From: Eric Sampson

Date: 27 Jun, 2013 15:26:06

Message: 2 of 6

"Timothy Siahaan" <timmy_fisika_ugm_99@yahoo.co.id> wrote in message <kqhjmf$a1t$1@newscl01ah.mathworks.com>...
> I have a strange experience.
>
> The problem is as follows. I need to minimize a function of several variables. Let the function be f=f(x1,x2,..,x5). (In my case, there are 5 variables).
> For that, I need to find the values of x1,...,x5 that makes the following functions:
> g1=diff(f,x1);
> g2=diff(f,x2);
> g3=diff(f,x3);
> g4=diff(f,x4);
> g5=diff(f,x5);
>
> The Newton-Raphson method is chosen for this problem, meaning that the Jacobian
> Jac(i,j)=diff(g i,xj)
> Should be calculated.
>
> The strange thing appears here:
> In order to do the calculation, the value of the jacobian matrix needs to be evaluated for a certain set of value of x1,x2,x3,x4,x5. After setting the values, I tried to calculate the value of the jacobian by defining a matrix JacVal as follows:
> JacVal=eval(Jac).
> Since my function is very long, I can understand that this process takes a very long time. However, by changing the approach of calculation, things become very different. If the matrix JacVal is defined component-wise as follows:
> for a=1:5
> for b=1:5
> JacVal(a,b)=eval(Jac(a,b));
> end
> end,
> the result comes out much quicker.
>
> I do not understand why it happens nor know whether the last approach is correct or not.
>
> Any explanation/help is appreciated.
>
> Best Regards,
>
> Timothy

Timothy, I don't exactly understand what you are doing, but I'm curious - why is 'eval' required in either case? What happens if you remove eval() in both cases and try the code again?

Subject: Strange experience in evaluating the value of a matrix

From: Timothy Siahaan

Date: 27 Jun, 2013 16:43:10

Message: 3 of 6

The question from E. Sampson is "Timothy, I don't exactly understand what you are doing, but I'm curious - why is 'eval' required in either case? What happens if you remove eval() in both cases and try the code again?"

I am trying to explain now. May be a bit boring, but if you are curious, kindly read it:
Actually what I am doing is to find a fit of my data to a model I am proposing. The model is described by a function with 5 parameters x1,x2,x3,x4,x5. Let say the function is A(t)=A(t,x1,x2,x3,x4,x5) (t is the experiment variable) and my data is C(t).
To do the fitting, I minimize the function f=f(x1,x2,x3,x4,x5) defined as
f=sum over t of (C(t)-A(t,x1,x2,x3,x4,x5))^2.
The condition of f to be minimized is that its partial derivatives vanish. However, the function is long and hand calculation to derive it is already tiring. To make life simple, I use matlab to calculate the derivative. For that, I just simply use the command
syms x1 x2 x3 x4 x5 real
followed by defining the g1,g2,g3,g4,g5 as I mentioned below. The values of the parameters x1,x2,x3,x4,x5 that makes the value of g1,g2,g3,g4,g5 are the values I am looking for:they minimize f.
For that, I use the Newton Raphson method, so I need to calculate the Jacobian of the functions g1,g2,g3,g3,g4,g4 with respect to x1,x2,x3,x4,x5, again with the help from Matlab. In the calculation, the iteration gives requires me to evaluate the values of g1,g2,g3,g4,g5 and the Jacobian at a given set x1,x2,x3,x4,x5.

That is why I use 'eval': given the values of x1,x2,x3,x4,x5, i need to find the value of the Jacobian. For that, I use
 eval(Jac)
where Jac is the Jacobian.

However, as I mentioned, defining the numerical value of Jac as JacVal via
JacVal=eval(Jac)
seems like torturing my computer, while doing
for a=1:5
  for b=1:5
   JacVal(a,b)=eval(Jac(a,b));
  end
end
seems to be more favorable for the computer.

Thank you for your attention

Timothy

"Eric Sampson" wrote in message <kqhlie$f95$1@newscl01ah.mathworks.com>...
> "Timothy Siahaan" <timmy_fisika_ugm_99@yahoo.co.id> wrote in message <kqhjmf$a1t$1@newscl01ah.mathworks.com>...
> > I have a strange experience.
> >
> > The problem is as follows. I need to minimize a function of several variables. Let the function be f=f(x1,x2,..,x5). (In my case, there are 5 variables).
> > For that, I need to find the values of x1,...,x5 that makes the following functions:
> > g1=diff(f,x1);
> > g2=diff(f,x2);
> > g3=diff(f,x3);
> > g4=diff(f,x4);
> > g5=diff(f,x5);
> >
> > The Newton-Raphson method is chosen for this problem, meaning that the Jacobian
> > Jac(i,j)=diff(g i,xj)
> > Should be calculated.
> >
> > The strange thing appears here:
> > In order to do the calculation, the value of the jacobian matrix needs to be evaluated for a certain set of value of x1,x2,x3,x4,x5. After setting the values, I tried to calculate the value of the jacobian by defining a matrix JacVal as follows:
> > JacVal=eval(Jac).
> > Since my function is very long, I can understand that this process takes a very long time. However, by changing the approach of calculation, things become very different. If the matrix JacVal is defined component-wise as follows:
> > for a=1:5
> > for b=1:5
> > JacVal(a,b)=eval(Jac(a,b));
> > end
> > end,
> > the result comes out much quicker.
> >
> > I do not understand why it happens nor know whether the last approach is correct or not.
> >
> > Any explanation/help is appreciated.
> >
> > Best Regards,
> >
> > Timothy
>
> Timothy, I don't exactly understand what you are doing, but I'm curious - why is 'eval' required in either case? What happens if you remove eval() in both cases and try the code again?

Subject: Strange experience in evaluating the value of a matrix

From: Eric Sampson

Date: 27 Jun, 2013 18:14:06

Message: 4 of 6

"Timothy Siahaan" <timmy_fisika_ugm_99@yahoo.co.id> wrote in message <kqhq2u$rbk$1@newscl01ah.mathworks.com>...
> The question from E. Sampson is "Timothy, I don't exactly understand what you are doing, but I'm curious - why is 'eval' required in either case? What happens if you remove eval() in both cases and try the code again?"
>
> I am trying to explain now. May be a bit boring, but if you are curious, kindly read it:
> Actually what I am doing is to find a fit of my data to a model I am proposing. The model is described by a function with 5 parameters x1,x2,x3,x4,x5. Let say the function is A(t)=A(t,x1,x2,x3,x4,x5) (t is the experiment variable) and my data is C(t).
> To do the fitting, I minimize the function f=f(x1,x2,x3,x4,x5) defined as
> f=sum over t of (C(t)-A(t,x1,x2,x3,x4,x5))^2.
> The condition of f to be minimized is that its partial derivatives vanish. However, the function is long and hand calculation to derive it is already tiring. To make life simple, I use matlab to calculate the derivative. For that, I just simply use the command
> syms x1 x2 x3 x4 x5 real
> followed by defining the g1,g2,g3,g4,g5 as I mentioned below. The values of the parameters x1,x2,x3,x4,x5 that makes the value of g1,g2,g3,g4,g5 are the values I am looking for:they minimize f.
> For that, I use the Newton Raphson method, so I need to calculate the Jacobian of the functions g1,g2,g3,g3,g4,g4 with respect to x1,x2,x3,x4,x5, again with the help from Matlab. In the calculation, the iteration gives requires me to evaluate the values of g1,g2,g3,g4,g5 and the Jacobian at a given set x1,x2,x3,x4,x5.
>
> That is why I use 'eval': given the values of x1,x2,x3,x4,x5, i need to find the value of the Jacobian. For that, I use
> eval(Jac)
> where Jac is the Jacobian.
>
> However, as I mentioned, defining the numerical value of Jac as JacVal via
> JacVal=eval(Jac)
> seems like torturing my computer, while doing
> for a=1:5
> for b=1:5
> JacVal(a,b)=eval(Jac(a,b));
> end
> end
> seems to be more favorable for the computer.
>
> Thank you for your attention
>
> Timothy
>
Thanks for the clearer explanation, your mention of SYMS makes things much clearer. I am not a user of the Symbolic Toolbox so I could be way off base, but look up the documentation for the SUBS function and give it a try instead of EVAL, and see if that helps...

Subject: Strange experience in evaluating the value of a matrix

From: Timothy Siahaan

Date: 27 Jun, 2013 18:20:37

Message: 5 of 6

Actually, before trying 'eval', I also tried 'subs'. They both do the same.

This is weird. I am still working on it.
"Eric Sampson" wrote in message <kqhvde$d7s$1@newscl01ah.mathworks.com>...
> "Timothy Siahaan" <timmy_fisika_ugm_99@yahoo.co.id> wrote in message <kqhq2u$rbk$1@newscl01ah.mathworks.com>...
> > The question from E. Sampson is "Timothy, I don't exactly understand what you are doing, but I'm curious - why is 'eval' required in either case? What happens if you remove eval() in both cases and try the code again?"
> >
> > I am trying to explain now. May be a bit boring, but if you are curious, kindly read it:
> > Actually what I am doing is to find a fit of my data to a model I am proposing. The model is described by a function with 5 parameters x1,x2,x3,x4,x5. Let say the function is A(t)=A(t,x1,x2,x3,x4,x5) (t is the experiment variable) and my data is C(t).
> > To do the fitting, I minimize the function f=f(x1,x2,x3,x4,x5) defined as
> > f=sum over t of (C(t)-A(t,x1,x2,x3,x4,x5))^2.
> > The condition of f to be minimized is that its partial derivatives vanish. However, the function is long and hand calculation to derive it is already tiring. To make life simple, I use matlab to calculate the derivative. For that, I just simply use the command
> > syms x1 x2 x3 x4 x5 real
> > followed by defining the g1,g2,g3,g4,g5 as I mentioned below. The values of the parameters x1,x2,x3,x4,x5 that makes the value of g1,g2,g3,g4,g5 are the values I am looking for:they minimize f.
> > For that, I use the Newton Raphson method, so I need to calculate the Jacobian of the functions g1,g2,g3,g3,g4,g4 with respect to x1,x2,x3,x4,x5, again with the help from Matlab. In the calculation, the iteration gives requires me to evaluate the values of g1,g2,g3,g4,g5 and the Jacobian at a given set x1,x2,x3,x4,x5.
> >
> > That is why I use 'eval': given the values of x1,x2,x3,x4,x5, i need to find the value of the Jacobian. For that, I use
> > eval(Jac)
> > where Jac is the Jacobian.
> >
> > However, as I mentioned, defining the numerical value of Jac as JacVal via
> > JacVal=eval(Jac)
> > seems like torturing my computer, while doing
> > for a=1:5
> > for b=1:5
> > JacVal(a,b)=eval(Jac(a,b));
> > end
> > end
> > seems to be more favorable for the computer.
> >
> > Thank you for your attention
> >
> > Timothy
> >
> Thanks for the clearer explanation, your mention of SYMS makes things much clearer. I am not a user of the Symbolic Toolbox so I could be way off base, but look up the documentation for the SUBS function and give it a try instead of EVAL, and see if that helps...

Subject: Strange experience in evaluating the value of a matrix

From: Steven_Lord

Date: 28 Jun, 2013 15:24:43

Message: 6 of 6



"Timothy Siahaan" <timmy_fisika_ugm_99@yahoo.co.id> wrote in message
news:kqhvpl$eb6$1@newscl01ah.mathworks.com...
> Actually, before trying 'eval', I also tried 'subs'. They both do the
> same.
>
> This is weird. I am still working on it.

Try converting the (probably LONG) symbolic expression into a function using
matlabFunction, then evaluate that function instead of the symbolic
expression.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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