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:
Computing the normal for a function?

Subject: Computing the normal for a function?

From: mlt

Date: 15 Dec, 2008 14:29:14

Message: 1 of 7

I have computed the gradient of the function sin(x):

x=0:0.1:5*pi/2;
f = sin(x);
FX = gradient(f);

I would now like to plot 'f' and the corresponding 'normals' (not tangents):

quiver(x, sin(x), x, FX)

But it looks more like I get the tangents.

How do I determine the normals and plot those instead?

Subject: Computing the normal for a function?

From: Dave

Date: 15 Dec, 2008 14:38:52

Message: 2 of 7

Wouldn't you just form the cross product of the tangent vector you
already have and the k vector where k is a vector perpendicular to the
plane of your computer monitor. So form the cross product of (Tx,Ty,0)
and (0, 0, 1).

Cheers,
Dave

On Dec 15, 9:29=A0am, "mlt" <a...@asd.com> wrote:
> I have computed the gradient of the function sin(x):
>
> x=3D0:0.1:5*pi/2;
> f =3D sin(x);
> FX =3D gradient(f);
>
> I would now like to plot 'f' and the corresponding 'normals' (not tangent=
s):
>
> quiver(x, sin(x), x, FX)
>
> But it looks more like I get the tangents.
>
> How do I determine the normals and plot those instead?

Subject: Computing the normal for a function?

From: mlt

Date: 15 Dec, 2008 14:53:30

Message: 3 of 7

Don't you mean:

normal = cross(T, (0,1,0))

since the function only have 1 variable (only defined over the x domain and
values belong to the the domain).


"Dave" <dspguy2@netscape.net> wrote in message
news:fd16be8f-771a-46d4-bf84-29b4f17bb20b@t26g2000prh.googlegroups.com...
Wouldn't you just form the cross product of the tangent vector you
already have and the k vector where k is a vector perpendicular to the
plane of your computer monitor. So form the cross product of (Tx,Ty,0)
and (0, 0, 1).

Cheers,
Dave

On Dec 15, 9:29 am, "mlt" <a...@asd.com> wrote:
> I have computed the gradient of the function sin(x):
>
> x=0:0.1:5*pi/2;
> f = sin(x);
> FX = gradient(f);
>
> I would now like to plot 'f' and the corresponding 'normals' (not
> tangents):
>
> quiver(x, sin(x), x, FX)
>
> But it looks more like I get the tangents.
>
> How do I determine the normals and plot those instead?

Subject: Computing the normal for a function?

From: mlt

Date: 15 Dec, 2008 15:07:47

Message: 4 of 7

Hm seems that crossing with (0,1,0) does not give the correct result
either. Here is the result when I cross with (0,0,1):

http://img132.imageshack.us/img132/755/matsf2.jpg

Only the normals on the top are correct, and for some reason the grow larger
when the x-values increase!

and the code:

step = 0.5;
x=0:step:9*pi/2;
z_dim = zeros(1,length(x));
y_dim = ones(1,length(x));
f = sin(x);
plot(x, f)
hold on

FX = gradient(f);
T = [x;FX;z_dim];
Y = [z_dim; z_dim; y_dim];

Normal = cross(T,Y);
NX = Normal(1,:);
NY = -1*Normal(2,:);

quiver(x, sin(x), NX, NY)





"mlt" <asdf@asd.com> wrote in message
news:49466f85$0$90267$14726298@news.sunsite.dk...
> Don't you mean:
>
> normal = cross(T, (0,1,0))
>
> since the function only have 1 variable (only defined over the x domain
> and values belong to the the domain).
>
>
> "Dave" <dspguy2@netscape.net> wrote in message
> news:fd16be8f-771a-46d4-bf84-29b4f17bb20b@t26g2000prh.googlegroups.com...
> Wouldn't you just form the cross product of the tangent vector you
> already have and the k vector where k is a vector perpendicular to the
> plane of your computer monitor. So form the cross product of (Tx,Ty,0)
> and (0, 0, 1).
>
> Cheers,
> Dave
>
> On Dec 15, 9:29 am, "mlt" <a...@asd.com> wrote:
>> I have computed the gradient of the function sin(x):
>>
>> x=0:0.1:5*pi/2;
>> f = sin(x);
>> FX = gradient(f);
>>
>> I would now like to plot 'f' and the corresponding 'normals' (not
>> tangents):
>>
>> quiver(x, sin(x), x, FX)
>>
>> But it looks more like I get the tangents.
>>
>> How do I determine the normals and plot those instead?
>

Subject: Computing the normal for a function?

From: Dave

Date: 15 Dec, 2008 18:14:26

Message: 5 of 7

On Dec 15, 10:07=A0am, "mlt" <a...@asd.com> wrote:
> Hm seems that crossing with =A0(0,1,0) does not give the correct result
> either. Here is the result when I cross with (0,0,1):
>
> http://img132.imageshack.us/img132/755/matsf2.jpg
>
> Only the normals on the top are correct, and for some reason the grow lar=
ger
> when the x-values increase!
>
> and the code:
>
> step =3D 0.5;
> x=3D0:step:9*pi/2;
> z_dim =3D zeros(1,length(x));
> y_dim =3D ones(1,length(x));
> f =3D sin(x);
> plot(x, f)
> hold on
>
> FX =3D gradient(f);
> T =3D [x;FX;z_dim];
> Y =3D [z_dim; z_dim; y_dim];
>
> Normal =3D cross(T,Y);
> NX =3D Normal(1,:);
> NY =3D -1*Normal(2,:);
>
> quiver(x, sin(x), NX, NY)
>
> "mlt" <a...@asd.com> wrote in message
>
> news:49466f85$0$90267$14726298@news.sunsite.dk...
>
> > Don't you mean:
>
> > normal =3D cross(T, (0,1,0))
>
> > since the function only have 1 variable (only defined over the x domain
> > and values belong to the the domain).
>
> > "Dave" <dspg...@netscape.net> wrote in message
> >news:fd16be8f-771a-46d4-bf84-29b4f17bb20b@t26g2000prh.googlegroups.com..=
.
> > Wouldn't you just form the cross product of the tangent vector you
> > already have and the k vector where k is a vector perpendicular to the
> > plane of your computer monitor. So form the cross product of (Tx,Ty,0)
> > and (0, 0, 1).
>
> > Cheers,
> > Dave
>
> > On Dec 15, 9:29 am, "mlt" <a...@asd.com> wrote:
> >> I have computed the gradient of the function sin(x):
>
> >> x=3D0:0.1:5*pi/2;
> >> f =3D sin(x);
> >> FX =3D gradient(f);
>
> >> I would now like to plot 'f' and the corresponding 'normals' (not
> >> tangents):
>
> >> quiver(x, sin(x), x, FX)
>
> >> But it looks more like I get the tangents.
>
> >> How do I determine the normals and plot those instead?

You're calculating the components of the tangent vector incorrectly.
The gradient() calculates df/dx. From your code you're setting df =3D FX
therefore dx =3D1 for all your vectors.

Note: You're not sampling the function quickly enough to get accurate
estimates of the derivative. Also gradient doesn't know about the
scaling of the x values. To get more realistic values try increasing
the sampling to 0.01 i.e. x=3D 0:0.01:2*pi;
f=3Dsin(x)
FX=3D gradient(f);
FX=3DFX/(x(2)-x(1)); %Note x(1) =3D 0 so you can just divide by x(2);

tan_vec =3D [ones(size(FX))' FX'];
quiver(x,sin(x), tan_vec(:,1)', tan_vec(:,2)');

To make it more viewable you can try:
tan_vec =3D tan_vec/3; %scale the tangent vectors by a fixed quantity.
Ind=3D1:10:length(x); % Subsample the indices - only plot every 10 th
sample
quiver(x(Ind),sin(x(Ind)), tan_vec(Ind,1)', tan_vec(Ind,2)');

Cheers,
David

Subject: Computing the normal for a function?

From: Dave

Date: 15 Dec, 2008 18:25:15

Message: 6 of 7

On Dec 15, 1:14=A0pm, Dave <dspg...@netscape.net> wrote:
> On Dec 15, 10:07=A0am, "mlt" <a...@asd.com> wrote:
>
>
>
> > Hm seems that crossing with =A0(0,1,0) does not give the correct result
> > either. Here is the result when I cross with (0,0,1):
>
> >http://img132.imageshack.us/img132/755/matsf2.jpg
>
> > Only the normals on the top are correct, and for some reason the grow l=
arger
> > when the x-values increase!
>
> > and the code:
>
> > step =3D 0.5;
> > x=3D0:step:9*pi/2;
> > z_dim =3D zeros(1,length(x));
> > y_dim =3D ones(1,length(x));
> > f =3D sin(x);
> > plot(x, f)
> > hold on
>
> > FX =3D gradient(f);
> > T =3D [x;FX;z_dim];
> > Y =3D [z_dim; z_dim; y_dim];
>
> > Normal =3D cross(T,Y);
> > NX =3D Normal(1,:);
> > NY =3D -1*Normal(2,:);
>
> > quiver(x, sin(x), NX, NY)
>
> > "mlt" <a...@asd.com> wrote in message
>
> >news:49466f85$0$90267$14726298@news.sunsite.dk...
>
> > > Don't you mean:
>
> > > normal =3D cross(T, (0,1,0))
>
> > > since the function only have 1 variable (only defined over the x doma=
in
> > > and values belong to the the domain).
>
> > > "Dave" <dspg...@netscape.net> wrote in message
> > >news:fd16be8f-771a-46d4-bf84-29b4f17bb20b@t26g2000prh.googlegroups.com=
...
> > > Wouldn't you just form the cross product of the tangent vector you
> > > already have and the k vector where k is a vector perpendicular to th=
e
> > > plane of your computer monitor. So form the cross product of (Tx,Ty,0=
)
> > > and (0, 0, 1).
>
> > > Cheers,
> > > Dave
>
> > > On Dec 15, 9:29 am, "mlt" <a...@asd.com> wrote:
> > >> I have computed the gradient of the function sin(x):
>
> > >> x=3D0:0.1:5*pi/2;
> > >> f =3D sin(x);
> > >> FX =3D gradient(f);
>
> > >> I would now like to plot 'f' and the corresponding 'normals' (not
> > >> tangents):
>
> > >> quiver(x, sin(x), x, FX)
>
> > >> But it looks more like I get the tangents.
>
> > >> How do I determine the normals and plot those instead?
>
> You're calculating the components of the tangent vector incorrectly.
> The gradient() calculates df/dx. From your code you're setting df =3D FX
> therefore dx =3D1 for all your vectors.
>
> Note: You're not sampling the function quickly enough to get accurate
> estimates of the derivative. Also gradient doesn't know about the
> scaling of the x values. To get more realistic values try increasing
> the sampling to 0.01 i.e. x=3D 0:0.01:2*pi;
> f=3Dsin(x)
> FX=3D gradient(f);
> FX=3DFX/(x(2)-x(1)); %Note x(1) =3D 0 so you can just divide by x(2);
>
> tan_vec =3D [ones(size(FX))' FX'];
> quiver(x,sin(x), tan_vec(:,1)', tan_vec(:,2)');
>
> To make it more viewable you can try:
> tan_vec =3D tan_vec/3; %scale the tangent vectors by a fixed quantity.
> Ind=3D1:10:length(x); % Subsample the indices - only plot every 10 th
> sample
> quiver(x(Ind),sin(x(Ind)), tan_vec(Ind,1)', tan_vec(Ind,2)');
>
> Cheers,
> David

Sorry,
To scale the vectors try the following:
quiver(x(Ind),sin(x(Ind)), tan_vec(Ind,1)', tan_vec(Ind,2)', 0.2);

Cheers,
Dave

Subject: Computing the normal for a function?

From: Dave

Date: 15 Dec, 2008 18:37:23

Message: 7 of 7

On Dec 15, 1:25=A0pm, Dave <dspg...@netscape.net> wrote:
> On Dec 15, 1:14=A0pm, Dave <dspg...@netscape.net> wrote:
>
>
>
> > On Dec 15, 10:07=A0am, "mlt" <a...@asd.com> wrote:
>
> > > Hm seems that crossing with =A0(0,1,0) does not give the correct resu=
lt
> > > either. Here is the result when I cross with (0,0,1):
>
> > >http://img132.imageshack.us/img132/755/matsf2.jpg
>
> > > Only the normals on the top are correct, and for some reason the grow=
 larger
> > > when the x-values increase!
>
> > > and the code:
>
> > > step =3D 0.5;
> > > x=3D0:step:9*pi/2;
> > > z_dim =3D zeros(1,length(x));
> > > y_dim =3D ones(1,length(x));
> > > f =3D sin(x);
> > > plot(x, f)
> > > hold on
>
> > > FX =3D gradient(f);
> > > T =3D [x;FX;z_dim];
> > > Y =3D [z_dim; z_dim; y_dim];
>
> > > Normal =3D cross(T,Y);
> > > NX =3D Normal(1,:);
> > > NY =3D -1*Normal(2,:);
>
> > > quiver(x, sin(x), NX, NY)
>
> > > "mlt" <a...@asd.com> wrote in message
>
> > >news:49466f85$0$90267$14726298@news.sunsite.dk...
>
> > > > Don't you mean:
>
> > > > normal =3D cross(T, (0,1,0))
>
> > > > since the function only have 1 variable (only defined over the x do=
main
> > > > and values belong to the the domain).
>
> > > > "Dave" <dspg...@netscape.net> wrote in message
> > > >news:fd16be8f-771a-46d4-bf84-29b4f17bb20b@t26g2000prh.googlegroups.c=
om...
> > > > Wouldn't you just form the cross product of the tangent vector you
> > > > already have and the k vector where k is a vector perpendicular to =
the
> > > > plane of your computer monitor. So form the cross product of (Tx,Ty=
,0)
> > > > and (0, 0, 1).
>
> > > > Cheers,
> > > > Dave
>
> > > > On Dec 15, 9:29 am, "mlt" <a...@asd.com> wrote:
> > > >> I have computed the gradient of the function sin(x):
>
> > > >> x=3D0:0.1:5*pi/2;
> > > >> f =3D sin(x);
> > > >> FX =3D gradient(f);
>
> > > >> I would now like to plot 'f' and the corresponding 'normals' (not
> > > >> tangents):
>
> > > >> quiver(x, sin(x), x, FX)
>
> > > >> But it looks more like I get the tangents.
>
> > > >> How do I determine the normals and plot those instead?
>
> > You're calculating the components of the tangent vector incorrectly.
> > The gradient() calculates df/dx. From your code you're setting df =3D F=
X
> > therefore dx =3D1 for all your vectors.
>
> > Note: You're not sampling the function quickly enough to get accurate
> > estimates of the derivative. Also gradient doesn't know about the
> > scaling of the x values. To get more realistic values try increasing
> > the sampling to 0.01 i.e. x=3D 0:0.01:2*pi;
> > f=3Dsin(x)
> > FX=3D gradient(f);
> > FX=3DFX/(x(2)-x(1)); %Note x(1) =3D 0 so you can just divide by x(2);
>
> > tan_vec =3D [ones(size(FX))' FX'];
> > quiver(x,sin(x), tan_vec(:,1)', tan_vec(:,2)');
>
> > To make it more viewable you can try:
> > tan_vec =3D tan_vec/3; %scale the tangent vectors by a fixed quantity.
> > Ind=3D1:10:length(x); % Subsample the indices - only plot every 10 th
> > sample
> > quiver(x(Ind),sin(x(Ind)), tan_vec(Ind,1)', tan_vec(Ind,2)');
>
> > Cheers,
> > David
>
> Sorry,
> To scale the vectors try the following:
> quiver(x(Ind),sin(x(Ind)), tan_vec(Ind,1)', tan_vec(Ind,2)', 0.2);
>
> Cheers,
> Dave

To plot the normals do the following:
tan_vec =3D[tan_vec zeros(size(tan_vec,1),1)];
Z=3Dzeros(size(tan_vec,1),3);
Z(:,3)=3D1; %create z direction vectors
nor=3Dcross(Z,tan_vec);
quiver(x,sin(x), nor(:,1)', nor(:,2)', 0.05);

Cheers,
David

Tags for this Thread

No tags are associated with 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