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:
How I can find the slope of a tangent line and plot that tangent line in matlab

Subject: How I can find the slope of a tangent line and plot that tangent line in matlab

From: Ion

Date: 22 Jan, 2009 16:10:04

Message: 1 of 11

Hello every one.I have the follwing problem.
I must write a function script to find the tangent line for the function f(x)=(A.*(x.^3))+B.*(x.^2)+(C.*x)+D.
Executing the scriptfile must create a plot with f(x) including the tangentline at the specific point.

Work only from the schedule below:

•The syntax of the function definition line, must be as follows:

function[slope]=tangentline(A,B,C,D, x, Location)

• A,B,C,D : These are the coefficients of the polynomial function.
• x :This is the name of the independent internal variable.x must be defined before calling this “tangentline”function. So don’t use a number!.
•Location :This variable tells you where the function must plot the tangentline. This location must be in the range of the independent variable.
For x=3, we type only "3" instead of "Location"

About the result, the output variable called "slope" will give you the slope of the tangent line at the located place.

Subject: How I can find the slope of a tangent line and plot that tangent line in matlab

From: Roger Stafford

Date: 22 Jan, 2009 16:24:01

Message: 2 of 11

"Ion " <xeradius@yahoo.com> wrote in message <gla5or$f3p$1@fred.mathworks.com>...
> Hello every one.I have the follwing problem.
> I must write a function script to find the tangent line for the function f(x)=(A.*(x.^3))+B.*(x.^2)+(C.*x)+D.
> Executing the scriptfile must create a plot with f(x) including the tangentline at the specific point.
>
> Work only from the schedule below:
>
> •The syntax of the function definition line, must be as follows:
>
> function[slope]=tangentline(A,B,C,D, x, Location)
>
> • A,B,C,D : These are the coefficients of the polynomial function.
> • x :This is the name of the independent internal variable.x must be defined before calling this “tangentline”function. So don’t use a number!.
> •Location :This variable tells you where the function must plot the tangentline. This location must be in the range of the independent variable.
> For x=3, we type only "3" instead of "Location"
>
> About the result, the output variable called "slope" will give you the slope of the tangent line at the located place.

  We won't do your homework for you, Ion. If you know how to dfferentiate polynomials and express the equation of a line knowing its slope and a point on it, then you should be able to solve the problem without any difficulty.

Roger Stafford

Subject: How I can find the slope of a tangent line and plot that tangent line in matlab

From: someone

Date: 22 Jan, 2009 16:52:01

Message: 3 of 11

"Ion " <xeradius@yahoo.com> wrote in message <gla5or$f3p$1@fred.mathworks.com>...
> Hello every one.I have the follwing problem.
> I must write a function script to find the tangent line for the function f(x)=(A.*(x.^3))+B.*(x.^2)+(C.*x)+D.
> Executing the scriptfile must create a plot with f(x) including the tangentline at the specific point.
>
> Work only from the schedule below:
>
> •The syntax of the function definition line, must be as follows:
>
> function[slope]=tangentline(A,B,C,D, x, Location)
>
> • A,B,C,D : These are the coefficients of the polynomial function.
> • x :This is the name of the independent internal variable.x must be defined before calling this “tangentline”function. So don’t use a number!.
> •Location :This variable tells you where the function must plot the tangentline. This location must be in the range of the independent variable.
> For x=3, we type only "3" instead of "Location"
>
> About the result, the output variable called "slope" will give you the slope of the tangent line at the located place.

What have you tried so far?
Where are you stuck?
What error messages are you getting?

Subject: How I can find the slope of a tangent line and plot that tangent line in matlab

From: Sean Larkin

Date: 22 Jan, 2009 20:08:02

Message: 4 of 11

"Ion " <xeradius@yahoo.com> wrote in message <gla5or$f3p$1@fred.mathworks.com>...
> Hello every one.I have the follwing problem.
> I must write a function script to find the tangent line for the function f(x)=(A.*(x.^3))+B.*(x.^2)+(C.*x)+D.
> Executing the scriptfile must create a plot with f(x) including the tangentline at the specific point.
>
> Work only from the schedule below:
>
> •The syntax of the function definition line, must be as follows:
>
> function[slope]=tangentline(A,B,C,D, x, Location)
>
> • A,B,C,D : These are the coefficients of the polynomial function.
> • x :This is the name of the independent internal variable.x must be defined before calling this “tangentline”function. So don’t use a number!.
> •Location :This variable tells you where the function must plot the tangentline. This location must be in the range of the independent variable.
> For x=3, we type only "3" instead of "Location"
>
> About the result, the output variable called "slope" will give you the slope of the tangent line at the located place.

How about:
Slope = 3*A*location^2+...;

Not sure I understand the question. If you just want the slope at a single point it's this simple.

Subject: How I can find the slope of a tangent line and plot that tangent line in matlab

From: Ion

Date: 23 Jan, 2009 14:02:02

Message: 5 of 11

This is my function script.
function [slop]=tangentline(A,B,C,D,x,Location)
% A,B,C,D : These are the coefficients of the polynomial function.
% x : This is the name of the independent internal
% variable. x must be defined before calling this “tangentline”function. So don’t use a number!.
% Location : This variable tells you where the function must
% plot the tangentline. This location must be in the
% range of the independent variable.
% For x=3, we type only "3" instead of "Location"


slop=3*A.*Location+2*B.*Location+C

y=x.^3+x.^2+x
plot(x,y)
hold on


b=(A.*Location.^3)+(B.*Location.^2)+(C.*Location)+D
y2=slop*(x-Location)+b
plot(x,y2,'r')
hold off

But the problem apers when i call for this function
For example
>> x=linspace(3,9);
>> tangentline(1,2,2,3,x,6)
My slope it will be calculated but my plot is not shoing the real tangent line of that function.
I wait for your answers.Thank you.

Subject: How I can find the slope of a tangent line and plot that tangent line in matlab

From: someone

Date: 23 Jan, 2009 14:51:02

Message: 6 of 11

"Ion " <xeradius@yahoo.com> wrote in message <glcikq$80r$1@fred.mathworks.com>...
> This is my function script.
> function [slop]=tangentline(A,B,C,D,x,Location)
> % A,B,C,D : These are the coefficients of the polynomial function.
> % x : This is the name of the independent internal
> % variable. x must be defined before calling this “tangentline”function. So don’t use a number!.
> % Location : This variable tells you where the function must
> % plot the tangentline. This location must be in the
> % range of the independent variable.
> % For x=3, we type only "3" instead of "Location"
>
>
> slop=3*A.*Location+2*B.*Location+C
>
> y=x.^3+x.^2+x
> plot(x,y)
> hold on
>
>
> b=(A.*Location.^3)+(B.*Location.^2)+(C.*Location)+D
> y2=slop*(x-Location)+b
> plot(x,y2,'r')
> hold off
>
> But the problem apers when i call for this function
> For example
> >> x=linspace(3,9);
> >> tangentline(1,2,2,3,x,6)
> My slope it will be calculated but my plot is not shoing the real tangent line of that function.
> I wait for your answers.Thank you.

% I think you need to replace the line:
slop=3*A.*Location+2*B.*Location+C
% with:
slop=3*A.*(Location^2)+2*B.*Location+C

% See if that works.

Subject: How I can find the slope of a tangent line and plot that tangent line in matlab

From: Ion

Date: 23 Jan, 2009 18:00:21

Message: 7 of 11

"someone" <someone@somewhere.net> wrote in message <glclgm$jtk$1@fred.mathworks.com>...
> "Ion " <xeradius@yahoo.com> wrote in message <glcikq$80r$1@fred.mathworks.com>...
> > This is my function script.
> > function [slop]=tangentline(A,B,C,D,x,Location)
> > % A,B,C,D : These are the coefficients of the polynomial function.
> > % x : This is the name of the independent internal
> > % variable. x must be defined before calling this “tangentline”function. So don’t use a number!.
> > % Location : This variable tells you where the function must
> > % plot the tangentline. This location must be in the
> > % range of the independent variable.
> > % For x=3, we type only "3" instead of "Location"
> >
> >
> > slop=3*A.*Location+2*B.*Location+C
> >
> > y=x.^3+x.^2+x
> > plot(x,y)
> > hold on
> >
> >
> > b=(A.*Location.^3)+(B.*Location.^2)+(C.*Location)+D
> > y2=slop*(x-Location)+b
> > plot(x,y2,'r')
> > hold off
> >
> > But the problem apers when i call for this function
> > For example
> > >> x=linspace(3,9);
> > >> tangentline(1,2,2,3,x,6)
> > My slope it will be calculated but my plot is not shoing the real tangent line of that function.
> > I wait for your answers.Thank you.
>
> % I think you need to replace the line:
> slop=3*A.*Location+2*B.*Location+C
> % with:
> slop=3*A.*(Location^2)+2*B.*Location+C
>
> % See if that works.

I tried and it acts in the same way.The problem is still unsolved.

Subject: How I can find the slope of a tangent line and plot that tangent line in matlab

From: someone

Date: 23 Jan, 2009 18:34:02

Message: 8 of 11

"Ion " <xeradius@yahoo.com> wrote in message <gld0jk$pfu$1@fred.mathworks.com>...
> "someone" <someone@somewhere.net> wrote in message <glclgm$jtk$1@fred.mathworks.com>...
> > "Ion " <xeradius@yahoo.com> wrote in message <glcikq$80r$1@fred.mathworks.com>...
> > > This is my function script.
> > > function [slop]=tangentline(A,B,C,D,x,Location)
> > > % A,B,C,D : These are the coefficients of the polynomial function.
> > > % x : This is the name of the independent internal
> > > % variable. x must be defined before calling this “tangentline”function. So don’t use a number!.
> > > % Location : This variable tells you where the function must
> > > % plot the tangentline. This location must be in the
> > > % range of the independent variable.
> > > % For x=3, we type only "3" instead of "Location"
> > >
> > >
> > > slop=3*A.*Location+2*B.*Location+C
> > >
> > > y=x.^3+x.^2+x
> > > plot(x,y)
> > > hold on
> > >
> > >
> > > b=(A.*Location.^3)+(B.*Location.^2)+(C.*Location)+D
> > > y2=slop*(x-Location)+b
> > > plot(x,y2,'r')
> > > hold off
> > >
> > > But the problem apers when i call for this function
> > > For example
> > > >> x=linspace(3,9);
> > > >> tangentline(1,2,2,3,x,6)
> > > My slope it will be calculated but my plot is not shoing the real tangent line of that function.
> > > I wait for your answers.Thank you.
> >
> > % I think you need to replace the line:
> > slop=3*A.*Location+2*B.*Location+C
> > % with:
> > slop=3*A.*(Location^2)+2*B.*Location+C
> >
> > % See if that works.
>
> I tried and it acts in the same way.The problem is still unsolved.
>
% OK, now you need to change:
y=x.^3+x.^2+x
% to
y=A*x.^3+B*x.^2+C*x + D
% to be consistent with b & slop

Subject: How I can find the slope of a tangent line and plot that tangent line in matlab

From: Ion

Date: 23 Jan, 2009 20:01:20

Message: 9 of 11

"someone" <someone@somewhere.net> wrote in message <gld2iq$9db$1@fred.mathworks.com>...
> "Ion " <xeradius@yahoo.com> wrote in message <gld0jk$pfu$1@fred.mathworks.com>...
> > "someone" <someone@somewhere.net> wrote in message <glclgm$jtk$1@fred.mathworks.com>...
> > > "Ion " <xeradius@yahoo.com> wrote in message <glcikq$80r$1@fred.mathworks.com>...
> > > > This is my function script.
> > > > function [slop]=tangentline(A,B,C,D,x,Location)
> > > > % A,B,C,D : These are the coefficients of the polynomial function.
> > > > % x : This is the name of the independent internal
> > > > % variable. x must be defined before calling this “tangentline”function. So don’t use a number!.
> > > > % Location : This variable tells you where the function must
> > > > % plot the tangentline. This location must be in the
> > > > % range of the independent variable.
> > > > % For x=3, we type only "3" instead of "Location"
> > > >
> > > >
> > > > slop=3*A.*Location+2*B.*Location+C
> > > >
> > > > y=x.^3+x.^2+x
> > > > plot(x,y)
> > > > hold on
> > > >
> > > >
> > > > b=(A.*Location.^3)+(B.*Location.^2)+(C.*Location)+D
> > > > y2=slop*(x-Location)+b
> > > > plot(x,y2,'r')
> > > > hold off
> > > >
> > > > But the problem apers when i call for this function
> > > > For example
> > > > >> x=linspace(3,9);
> > > > >> tangentline(1,2,2,3,x,6)
> > > > My slope it will be calculated but my plot is not shoing the real tangent line of that function.
> > > > I wait for your answers.Thank you.
> > >
> > > % I think you need to replace the line:
> > > slop=3*A.*Location+2*B.*Location+C
> > > % with:
> > > slop=3*A.*(Location^2)+2*B.*Location+C
> > >
> > > % See if that works.
> >
> > I tried and it acts in the same way.The problem is still unsolved.
> >
> % OK, now you need to change:
> y=x.^3+x.^2+x
> % to
> y=A*x.^3+B*x.^2+C*x + D
> % to be consistent with b & slop
It dosen't change anything.My plot looks the same but i get a diffrent result for the slope.Thank you for your help.

Subject: How I can find the slope of a tangent line and plot that tangent line in matlab

From: someone

Date: 23 Jan, 2009 20:38:01

Message: 10 of 11

"Ion " <xeradius@yahoo.com> wrote in message <gld7mg$bm7$1@fred.mathworks.com>...
> "someone" <someone@somewhere.net> wrote in message <gld2iq$9db$1@fred.mathworks.com>...
> > "Ion " <xeradius@yahoo.com> wrote in message <gld0jk$pfu$1@fred.mathworks.com>...
> > > "someone" <someone@somewhere.net> wrote in message <glclgm$jtk$1@fred.mathworks.com>...
> > > > "Ion " <xeradius@yahoo.com> wrote in message <glcikq$80r$1@fred.mathworks.com>...
> > > > > This is my function script.
> > > > > function [slop]=tangentline(A,B,C,D,x,Location)
> > > > > % A,B,C,D : These are the coefficients of the polynomial function.
> > > > > % x : This is the name of the independent internal
> > > > > % variable. x must be defined before calling this “tangentline”function. So don’t use a number!.
> > > > > % Location : This variable tells you where the function must
> > > > > % plot the tangentline. This location must be in the
> > > > > % range of the independent variable.
> > > > > % For x=3, we type only "3" instead of "Location"
> > > > >
> > > > >
> > > > > slop=3*A.*Location+2*B.*Location+C
> > > > >
> > > > > y=x.^3+x.^2+x
> > > > > plot(x,y)
> > > > > hold on
> > > > >
> > > > >
> > > > > b=(A.*Location.^3)+(B.*Location.^2)+(C.*Location)+D
> > > > > y2=slop*(x-Location)+b
> > > > > plot(x,y2,'r')
> > > > > hold off
> > > > >
> > > > > But the problem apers when i call for this function
> > > > > For example
> > > > > >> x=linspace(3,9);
> > > > > >> tangentline(1,2,2,3,x,6)
> > > > > My slope it will be calculated but my plot is not shoing the real tangent line of that function.
> > > > > I wait for your answers.Thank you.
> > > >
> > > > % I think you need to replace the line:
> > > > slop=3*A.*Location+2*B.*Location+C
> > > > % with:
> > > > slop=3*A.*(Location^2)+2*B.*Location+C
> > > >
> > > > % See if that works.
> > >
> > > I tried and it acts in the same way.The problem is still unsolved.
> > >
> > % OK, now you need to change:
> > y=x.^3+x.^2+x
> > % to
> > y=A*x.^3+B*x.^2+C*x + D
> > % to be consistent with b & slop
> It dosen't change anything.My plot looks the same but i get a diffrent result for the slope.Thank you for your help.

% Then I guess I don't know what you are trying to do.
% If I code this:

% dummy data:
A = 1;
B = 2;
C = 3;
D = 4;
Location = 5;
x = linspace(3,9);
% plot polynomial:
y=A*x.^3+B*x.^2+C*x + D;
plot(x,y)
hold on
% find slope at Location:
slop=3*A.*(Location^2)+2*B.*Location+C;
% find y at Location:
b=(A.*Location.^3)+(B.*Location.^2)+(C.*Location)+D;
% plot slope:
y2=slop*(x-Location)+b;
plot(x,y2,'r')
hold off

% I get the plot I thought you wanted.

Subject: How I can find the slope of a tangent line and plot that tangent line in matlab

From: Ion

Date: 24 Jan, 2009 00:01:05

Message: 11 of 11

"someone" <someone@somewhere.net> wrote in message <gld9r9$378$1@fred.mathworks.com>...
> "Ion " <xeradius@yahoo.com> wrote in message <gld7mg$bm7$1@fred.mathworks.com>...
> > "someone" <someone@somewhere.net> wrote in message <gld2iq$9db$1@fred.mathworks.com>...
> > > "Ion " <xeradius@yahoo.com> wrote in message <gld0jk$pfu$1@fred.mathworks.com>...
> > > > "someone" <someone@somewhere.net> wrote in message <glclgm$jtk$1@fred.mathworks.com>...
> > > > > "Ion " <xeradius@yahoo.com> wrote in message <glcikq$80r$1@fred.mathworks.com>...
> > > > > > This is my function script.
> > > > > > function [slop]=tangentline(A,B,C,D,x,Location)
> > > > > > % A,B,C,D : These are the coefficients of the polynomial function.
> > > > > > % x : This is the name of the independent internal
> > > > > > % variable. x must be defined before calling this “tangentline”function. So don’t use a number!.
> > > > > > % Location : This variable tells you where the function must
> > > > > > % plot the tangentline. This location must be in the
> > > > > > % range of the independent variable.
> > > > > > % For x=3, we type only "3" instead of "Location"
> > > > > >
> > > > > >
> > > > > > slop=3*A.*Location+2*B.*Location+C
> > > > > >
> > > > > > y=x.^3+x.^2+x
> > > > > > plot(x,y)
> > > > > > hold on
> > > > > >
> > > > > >
> > > > > > b=(A.*Location.^3)+(B.*Location.^2)+(C.*Location)+D
> > > > > > y2=slop*(x-Location)+b
> > > > > > plot(x,y2,'r')
> > > > > > hold off
> > > > > >
> > > > > > But the problem apers when i call for this function
> > > > > > For example
> > > > > > >> x=linspace(3,9);
> > > > > > >> tangentline(1,2,2,3,x,6)
> > > > > > My slope it will be calculated but my plot is not shoing the real tangent line of that function.
> > > > > > I wait for your answers.Thank you.
> > > > >
> > > > > % I think you need to replace the line:
> > > > > slop=3*A.*Location+2*B.*Location+C
> > > > > % with:
> > > > > slop=3*A.*(Location^2)+2*B.*Location+C
> > > > >
> > > > > % See if that works.
> > > >
> > > > I tried and it acts in the same way.The problem is still unsolved.
> > > >
> > > % OK, now you need to change:
> > > y=x.^3+x.^2+x
> > > % to
> > > y=A*x.^3+B*x.^2+C*x + D
> > > % to be consistent with b & slop
> > It dosen't change anything.My plot looks the same but i get a diffrent result for the slope.Thank you for your help.
>
> % Then I guess I don't know what you are trying to do.
> % If I code this:
>
> % dummy data:
> A = 1;
> B = 2;
> C = 3;
> D = 4;
> Location = 5;
> x = linspace(3,9);
> % plot polynomial:
> y=A*x.^3+B*x.^2+C*x + D;
> plot(x,y)
> hold on
> % find slope at Location:
> slop=3*A.*(Location^2)+2*B.*Location+C;
> % find y at Location:
> b=(A.*Location.^3)+(B.*Location.^2)+(C.*Location)+D;
> % plot slope:
> y2=slop*(x-Location)+b;
> plot(x,y2,'r')
> hold off
>
> % I get the plot I thought you wanted.
Yes now it works. I don't know where it was my problem but thank you a lot.
That's the plot i whant.Thank's again.

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