The following issues in the MATLAB code are causing the errors:
- Since "x0", "y0" and "m" are supposed to be handled numerically, they should not be defined as symbolic variables.
- To find the slope function, "yprime" should be the first derivative of "f(x)" and for its evaluation, you can utilize the "subs" function finding the slope at "x0".
- As of MATLAB R2024a, the "ezplot" function has been deprecated, hence "fplot" should be used instead for compatibility.
Here is the corrected code for computing the Tangent Line and Normal Line of a curve about a given point:
% This program computes for the Tangent Line / Normal Line of a curve about a given point
% Define x for symbolic processing
syms x f(x)
% 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 for x
% FIND the slope function
yprime = diff(f, x); % Solve for the first derivative
% Determine the slope at the given x0
m = double(subs(yprime, x, x0)); % Evaluate the slope at x0
% 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
figure;
hold on;
fplot(f, [-15, 15], 'b');
plot(x0, y0, 'r*');
text(x0, y0 + 100, "Point of Tangency");
fplot(ytangent, [-15, 15], 'm');
text(5, 5, ["y(Tangent)=", string(ytangent)]);
fplot(ynormal, [-15, 15], 'c');
text(-5, -50, ["y(Normal)=", string(ynormal)]);
grid on;
title("Tangent Line and Normal Line");
hold off;
For more information regarding the "fplot" and "subs" functions, refer to the following documentation: