Derivatives using second diff

6 views (last 30 days)
MJ on 20 Sep 2022
Edited: MJ on 20 Sep 2022
% This program computes for the Tangent Line / Normal Line of a curve % about a given point
%define x for symbolic processing
syms x y f(x) x0 y0 m
%IDENTIFY the function f(x)
f(x) = (4*x^2)-(2*x) + 1;
%DETERMINE the point of tangency (This will be a random point)
x0=randi([-5,5])
%SOLVE for the Ordinate of the point of tangency
y0=f(x0); %Evaluate y given value fo x
y = (4*x0^2)-(2*x0) + 1;
%FIND the slope function
yprime(x) = diff(f(x)); %Solve for the first derivative
%Determine the slope at the given x0
m(x)= diff(y); %Evaluate the slope
%Solve the equation of the tangent line
ytangent= m*(x-x0)+y0
%Solve the Equation of the normal line
ynormal= (-1.\m)*(x-x0)+y0
%DISPLAYING RESULTS
fprintf('The tangent line to f(x)=%s at (%.2f, %.2f) is y = %s \n',string(f(x)),x0,y0, string(ytangent))
fprintf('The normal line to f(x)=%s at (%.2f, %.2f) is y = %s \n',string(f(x)),x0,y0, string(ynormal))
%PLOTTING
g1=ezplot(f,[-15,15]);
set(g1,'color','b')
grid on
hold on
plot(x0,y0,'r*')
text(x0+1,y0,"Point of Tangency")
g2=ezplot(ytangent,[-15,15]);
text(5,5,["y(Tangent)=", string(ytangent)])
pause(1)
set(g2,'color','m')
pause(1)
g3=ezplot(ynormal,[-15,15]);
text(3,3,["y(Normal)=", string(ynormal)])
set(g3,'color','c');
title("Tangent Line and Normal Line")
Unable to perform assignment because the left and right sides have a different number of elements.
Variable ytangent has an incorrect value.
Variable ynormal has an incorrect value.

Dyuman Joshi on 20 Sep 2022
syms x y f(x) x0 y0 m
%IDENTIFY the function f(x)
f(x) = (4*x^2)-(2*x) + 1;
%DETERMINE the point of tangency (This will be a random point)
x0=randi([-5,5]);
%SOLVE for the Ordinate of the point of tangency
y0=f(x0); %Evaluate y given value fo x
y =(4*x0^2)-(2*x0) + 1
y = 43
y is a constant value, so diff(y) will be 0
If you want to find out the numerical value of slope, just plug it in the equation
m(x) = diff(f(x))
m(x) =
m(x0)
ans =
ytangent= m*(x-x0)+y0
ytangent(x) =
ynormal= (-1.\m)*(x-x0)+y0
ynormal(x) =
%DISPLAYING RESULTS
fprintf('The tangent line to f(x)=%s at (%.2f, %.2f) is y = %s \n',string(f(x)),x0,y0, string(ytangent))
The tangent line to f(x)=4*x^2 - 2*x + 1 at (-3.00, 43.00) is y = (8*x - 2)*(x + 3) + 43
fprintf('The normal line to f(x)=%s at (%.2f, %.2f) is y = %s \n',string(f(x)),x0,y0, string(ynormal))
The normal line to f(x)=4*x^2 - 2*x + 1 at (-3.00, 43.00) is y = 43 - (8*x - 2)*(x + 3)
g1=ezplot(f,[-15,15]);
set(g1,'color','b')
grid on
hold on
plot(x0,y0,'r*')
text(x0+1,y0,"Point of Tangency")
g2=ezplot(ytangent,[-15,15]);
text(5,5,["y(Tangent)=", string(ytangent)])
pause(1)
set(g2,'color','m')
pause(1)
g3=ezplot(ynormal,[-15,15]);
text(3,3,["y(Normal)=", string(ynormal)])
set(g3,'color','c');
title("Tangent Line and Normal Line")

Categories

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

Community Treasure Hunt

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

Start Hunting!

Translated by