Error says: Variable yprime must be of data type symfun. It is currently of type sym. Check where the variable is assigned a value.

7 views (last 30 days)
% This program computes for the Tangent Line / Normal Line of a curve % about a given point
%define x for symbolic processing
syms 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 fo x
y=73
%FIND the slope function
yprime= diff(4*x^2-2*x+1) %Solve for the first derivative
%Determine the slope at the given x0
m=sym(-42) %Evaluate the slope
%Solve the equation of the tangent line
ytangent = sym(-42*x+1/42)
%Solve the Equation of the normal line
ynormal = sym(((1/42)*x)+215/42)
%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")
Variable yprime must be of data type symfun. It is currently of type sym. Check where the variable is assigned a value.
Variable m must be of data type symfun. It is currently of type sym. Check where the variable is assigned a value.
Variable ytangent must be of data type symfun. It is currently of type sym. Check where the variable is assigned a value.
Variable ynormal must be of data type symfun. It is currently of type sym. Check where the variable is assigned a value.
  3 Comments

Sign in to comment.

Answers (1)

Dyuman Joshi
Dyuman Joshi on 28 Mar 2023
Edited: Dyuman Joshi on 28 Mar 2023
Looks like you are working on an older version of MATLAB, as your code runs without an error on R2021b.
1 - Use f directly to find the derivative of the function.
2 - Since x is already defined as a symolic variable, you do not need to use sym() for defining ytangent and ynormal.
3 - Additionally, the variable m and the equations ytangent and ynormal are constant, which they shouldn't be, as x0 is not a fixed value/point. Modify the values accordingly.
4 - ezplot is not recommended, use fplot instead.
5 - You might want to rearrange the text on the plot.
syms 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 fo x
y=73;
%FIND the slope function
yprime= diff(f,x) %Solve for the first derivative
yprime(x) = 
%Determine the slope at the given x0
m=yprime(x0); %sym(-42); %Evaluate the slope
%Solve the equation of the tangent line
ytangent = -42*x+1/42
ytangent = 
%Solve the Equation of the normal line
ynormal = ((1/42)*x)+215/42
ynormal = 
%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 (0.00, 1.00) is y = 1/42 - 42*x
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 (0.00, 1.00) is y = x/42 + 215/42
%PLOTTING
%use fplot
g1=fplot(f,[-15,15]);
set(g1,'color','b')
grid on
hold on
plot(x0,y0,'r*')
text(x0+1,y0,"Point of Tangency")
g2=fplot(ytangent,[-15,15]);
text(5,5,["y(Tangent)=", string(ytangent)])
pause(1)
set(g2,'color','m')
pause(1)
g3=fplot(ynormal,[-15,15]);
text(3,3,["y(Normal)=", string(ynormal)])
set(g3,'color','c');
title("Tangent Line and Normal Line")

Community Treasure Hunt

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

Start Hunting!