How to find the equation of a line tangent to the curve at point (x,y)?

6 views (last 30 days)
So I have to find the equation of a line tangent to the curve y=x*cos(x) at the point (pi, -pi) and then graph the curve and tangent in Matlab.
This is what I have so far:
syms x y g m
y=x*cos(x)
g=diff(y)
x=pi
m=solve(g)
%Graph Curve and Tangent
ezplot('(x*cos(x))')
But I get the error:
Warning: Cannot solve symbolically. Returning a numeric approximation instead. In solve (line 303)
So I'm stuck. Anyhelp on what to do next would be greatly appreciated.

Answers (1)

Star Strider
Star Strider on 16 Sep 2015
You need to use the symbolic function capabilities of the Symbolic Math Toolbox:
syms y(x) Dy(x)
y(x) = x*cos(x);
Dy(x) = diff(y);
y1 = y(-pi);
Dy1 = Dy(-pi);
y2 = y(pi);
Dy2 = Dy(pi);
This code gives you the slopes and dependent variable values of the function at both values of the independent variable. That’s enough information to find the equations of the lines at [-pi, pi].

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!