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:
symbolic toolbox matlabFunction

Subject: symbolic toolbox matlabFunction

From: Floris Zoutman

Date: 19 Mar, 2010 16:17:05

Message: 1 of 4

I have a piece of code that looks something like this:
x=zeros(N_x,1);
y=zeros(N_y,1);
sym(x);
sym(y);
H=a differentiable scalar function of vectors y and x;
H_x=simplify(jacobian(H,x));
H_y=simplify(jacobian(H,y));
f_x=matlabFunction(H_x);
f_y=matlabFunction(H_y);
for i=many values of x and y
x=N_x*1 vector with some values for x
y=N_y*1 vector with some values for y
derivatives_x(:,i)=f_x(x,y);
derivatives_y(:,i)=f_y(x,y);
end

Now my problem is that depending on the functional form of H, f_x and f_y could be a function of only x or only y or a function of neither. In that case the final two lines of my code cannot be executed. Is there any way to work around this problem? Suppose that f_x is a function of x only than I would be very happy if f_x would look like:
f_x(x,y)=g(x)+0*y
but I do not know how to get to something like that. Does anybody have any suggestions? Many thanks in advance.

With kind regards,

Floris

Subject: symbolic toolbox matlabFunction

From: Steven Lord

Date: 19 Mar, 2010 17:04:03

Message: 2 of 4


"Floris Zoutman" <fzoutman@hotmail.com> wrote in message
news:ho0821$t5t$1@fred.mathworks.com...
>I have a piece of code that looks something like this:
> x=zeros(N_x,1);
> y=zeros(N_y,1);
> sym(x);
> sym(y);
> H=a differentiable scalar function of vectors y and x;
> H_x=simplify(jacobian(H,x)); H_y=simplify(jacobian(H,y));
> f_x=matlabFunction(H_x);
> f_y=matlabFunction(H_y);
> for i=many values of x and y
> x=N_x*1 vector with some values for x
> y=N_y*1 vector with some values for y
> derivatives_x(:,i)=f_x(x,y);
> derivatives_y(:,i)=f_y(x,y);
> end
>
> Now my problem is that depending on the functional form of H, f_x and f_y
> could be a function of only x or only y or a function of neither. In that
> case the final two lines of my code cannot be executed. Is there any way
> to work around this problem? Suppose that f_x is a function of x only than
> I would be very happy if f_x would look like:
> f_x(x,y)=g(x)+0*y but I do not know how to get to something like that.
> Does anybody have any suggestions? Many thanks in advance.

Use the 'vars' parameter for matlabFunction. There's an example on the
reference page that uses 'vars' to control the order of the parameters; you
can also use it to require that the function accept input variables that it
doesn't depend on, in order to make its signature match a desired signature.

http://www.mathworks.com/access/helpdesk/help/toolbox/symbolic/matlabfunction.html

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

Subject: symbolic toolbox matlabFunction

From: Roger Stafford

Date: 19 Mar, 2010 17:05:05

Message: 3 of 4

"Floris Zoutman" <fzoutman@hotmail.com> wrote in message <ho0821$t5t$1@fred.mathworks.com>...
>
> Now my problem is that depending on the functional form of H, f_x and f_y could be a function of only x or only y or a function of neither. In that case the final two lines of my code cannot be executed.
> ........

  Your symbolic toolbox should be smart enough to get symbolic zeros in those cases you describe. My version is.

Roger Stafford

Subject: symbolic toolbox matlabFunction

From: Floris Zoutman

Date: 23 Mar, 2010 10:55:22

Message: 4 of 4

Thanks Steven and Roger, the vars command works!
"Steven Lord" <slord@mathworks.com> wrote in message <ho0apr$g24$1@fred.mathworks.com>...
>
> "Floris Zoutman" <fzoutman@hotmail.com> wrote in message
> news:ho0821$t5t$1@fred.mathworks.com...
> >I have a piece of code that looks something like this:
> > x=zeros(N_x,1);
> > y=zeros(N_y,1);
> > sym(x);
> > sym(y);
> > H=a differentiable scalar function of vectors y and x;
> > H_x=simplify(jacobian(H,x)); H_y=simplify(jacobian(H,y));
> > f_x=matlabFunction(H_x);
> > f_y=matlabFunction(H_y);
> > for i=many values of x and y
> > x=N_x*1 vector with some values for x
> > y=N_y*1 vector with some values for y
> > derivatives_x(:,i)=f_x(x,y);
> > derivatives_y(:,i)=f_y(x,y);
> > end
> >
> > Now my problem is that depending on the functional form of H, f_x and f_y
> > could be a function of only x or only y or a function of neither. In that
> > case the final two lines of my code cannot be executed. Is there any way
> > to work around this problem? Suppose that f_x is a function of x only than
> > I would be very happy if f_x would look like:
> > f_x(x,y)=g(x)+0*y but I do not know how to get to something like that.
> > Does anybody have any suggestions? Many thanks in advance.
>
> Use the 'vars' parameter for matlabFunction. There's an example on the
> reference page that uses 'vars' to control the order of the parameters; you
> can also use it to require that the function accept input variables that it
> doesn't depend on, in order to make its signature match a desired signature.
>
> http://www.mathworks.com/access/helpdesk/help/toolbox/symbolic/matlabfunction.html
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

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