How to Draw a Tangent Line in Matlab

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

Discussion in 'MATLAB' started by Ion, Jan 22, 2009.

  1. 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.

    1. Advertisements

  2. 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

    1. Advertisements

  3. 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.

  4. 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 exampleMy 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.

  5. % 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.

  6. I tried and it acts in the same way.The problem is still unsolved.
  7. % 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
  8. It dosen't change anything.My plot looks the same but i get a diffrent result for the slope.Thank you for your help.
  9. % 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.

  10. 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.
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.

How to Draw a Tangent Line in Matlab

Source: https://www.math-forums.com/threads/how-i-can-find-the-slope-of-a-tangent-line-and-plot-that-tangent-line-in-matlab.193115/

0 Response to "How to Draw a Tangent Line in Matlab"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel