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:
Question about 'subs'

Subject: Question about 'subs'

From: Xu

Date: 11 Nov, 2012 21:02:13

Message: 1 of 6

Hello everyone,

For example, my function is x1^2*t^2 + x2^2*t^2+ .... + xn^2*t^2 + t^2, where n is any integer, I want to get the derivative wrt t at x1=1, x2=1, ..., xn=1, t=1.

For example, if n=2, f = x1^2*t^2 + x2^2*t^2 + t^2, I can use 'subs' to get the derivative
answer=subs(diff(f,t), {x1,x2,t}, {1,1,1}) .

However, since n is not fixed, how can I get the solution if n is very large, I cannot input
subs(diff(f,t), {x1,x2,x3.......x100}).

I tried
subs(diff(f,t), {x,t}, {zeros(1,100),1})
Does not work.

I will appreciate if anyone can give me some advice. Thanks.

Subject: Question about 'subs'

From: Nasser M. Abbasi

Date: 11 Nov, 2012 22:02:34

Message: 2 of 6

On 11/11/2012 3:02 PM, Xu wrote:
> Hello everyone,
>
> For example, my function is x1^2*t^2 + x2^2*t^2+ .... + xn^2*t^2 + t^2, where n is any integer,
>I want to get the derivative wrt t at x1=1, x2=1, ..., xn=1, t=1.
>
> For example, if n=2, f = x1^2*t^2 + x2^2*t^2 + t^2, I can use 'subs' to get the derivative
> answer=subs(diff(f,t), {x1,x2,t}, {1,1,1}) .
>
> However, since n is not fixed, how can I get the solution if n is very large, I cannot input
> subs(diff(f,t), {x1,x2,x3.......x100}).
>
> I tried
> subs(diff(f,t), {x,t}, {zeros(1,100),1})
> Does not work.
>
> I will appreciate if anyone can give me some advice. Thanks.
>


Isn't your function simply

             f(t)=(x1^2+x2^2+....+xn^2)*t^2?

then the derivative is

           2*t*(x1^2+x2^2+....+xn^2)

So you do not need to call diff at all. Simply subs values
for t and all the x's in the above expression. Easier to use
numerics here.

If your x values are in a vector X for example, you can do
something like

           sum(X.^2) * 2*t

--Nasser

Subject: Question about 'subs'

From: Xu

Date: 11 Nov, 2012 22:26:13

Message: 3 of 6

"Nasser M. Abbasi" <nma@12000.org> wrote in message <k7p79m$lbr$1@speranza.aioe.org>...
> On 11/11/2012 3:02 PM, Xu wrote:
> > Hello everyone,
> >
> > For example, my function is x1^2*t^2 + x2^2*t^2+ .... + xn^2*t^2 + t^2, where n is any integer,
> >I want to get the derivative wrt t at x1=1, x2=1, ..., xn=1, t=1.
> >
> > For example, if n=2, f = x1^2*t^2 + x2^2*t^2 + t^2, I can use 'subs' to get the derivative
> > answer=subs(diff(f,t), {x1,x2,t}, {1,1,1}) .
> >
> > However, since n is not fixed, how can I get the solution if n is very large, I cannot input
> > subs(diff(f,t), {x1,x2,x3.......x100}).
> >
> > I tried
> > subs(diff(f,t), {x,t}, {zeros(1,100),1})
> > Does not work.
> >
> > I will appreciate if anyone can give me some advice. Thanks.
> >
>
>
> Isn't your function simply
>
> f(t)=(x1^2+x2^2+....+xn^2)*t^2?
>
> then the derivative is
>
> 2*t*(x1^2+x2^2+....+xn^2)
>
> So you do not need to call diff at all. Simply subs values
> for t and all the x's in the above expression. Easier to use
> numerics here.
>
> If your x values are in a vector X for example, you can do
> something like
>
> sum(X.^2) * 2*t
>
> --Nasser

No, that was my example. My function is much more complex. So I need to use
subs(diff(f,t), {X,t}, (X-value, t-value))
where X is a vector.

Subject: Question about 'subs'

From: Eduardo Montoya

Date: 12 Nov, 2012 00:09:18

Message: 4 of 6

> No, that was my example. My function is much more complex. So I need to use
> subs(diff(f,t), {X,t}, (X-value, t-value))
> where X is a vector.

syms t
x = sym('x',[3 1])

f = x(1)^2*t^2 + x(2)^2*t^2 + x(3)^2*t^2

subs(subs(diff(f,t),x,ones(3,1)),t,2)

Subject: Question about 'subs'

From: Xu

Date: 12 Nov, 2012 01:19:14

Message: 5 of 6

"Eduardo Montoya" wrote in message <k7pene$ss7$1@newscl01ah.mathworks.com>...
> > No, that was my example. My function is much more complex. So I need to use
> > subs(diff(f,t), {X,t}, (X-value, t-value))
> > where X is a vector.
>
> syms t
> x = sym('x',[3 1])
>
> f = x(1)^2*t^2 + x(2)^2*t^2 + x(3)^2*t^2
>
> subs(subs(diff(f,t),x,ones(3,1)),t,2)


Thanks a lot!!!

Subject: Question about 'subs'

From: Xu

Date: 12 Nov, 2012 01:20:19

Message: 6 of 6

"Eduardo Montoya" wrote in message <k7pene$ss7$1@newscl01ah.mathworks.com>...
> > No, that was my example. My function is much more complex. So I need to use
> > subs(diff(f,t), {X,t}, (X-value, t-value))
> > where X is a vector.
>
> syms t
> x = sym('x',[3 1])
>
> f = x(1)^2*t^2 + x(2)^2*t^2 + x(3)^2*t^2
>
> subs(subs(diff(f,t),x,ones(3,1)),t,2)

Thanks a lot

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