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

12 次查看(过去 30 天)
% 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 个评论
Dyuman Joshi
Dyuman Joshi 2023-3-28
I specifically mentioned not to post a picture and you posted a picture. Smh.
Copy paste the text of the code.
And "And copy paste your full error message, that means all of the red text."

请先登录,再进行评论。

回答(1 个)

Dyuman Joshi
Dyuman Joshi 2023-3-28
编辑:Dyuman Joshi 2023-3-28
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")

类别

Help CenterFile Exchange 中查找有关 Formula Manipulation and Simplification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by