Error using feval Function to evaluate must be represented as a string scalar, character vector, or function_handle object.

24 views (last 30 days)
f(t)=[-cos(t), -sin(t), 2*cos(t)^3 - 7*cos(t)*sin(t)^2]
feval(f(t),3.14)
I'd like to evaluate the vector value function above at 3.14, but I get the error message in the subject line. How do I fix this?
  1 Comment
William Gibson
William Gibson on 4 Feb 2023
The error messages are listed below:
Error using feval
Function to evaluate must be represented as a string scalar, character vector, or function_handle object.
Error in untitled2 (line 2)
feval(f(t),3.14)

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 4 Feb 2023
Another approach, if you don't want to work symbolically as @Walter Roberson did, is to create an anonymous function. All I needed to do was move the equals sign and add an @ symbol.
f = @(t) [-cos(t), -sin(t), 2*cos(t)^3 - 7*cos(t)*sin(t)^2]
f = function_handle with value:
@(t)[-cos(t),-sin(t),2*cos(t)^3-7*cos(t)*sin(t)^2]
feval(f,3.14)
ans = 1×3
1.0000 -0.0016 -2.0000
But with an anonymous function you don't even need feval.
y = f(3.14)
y = 1×3
1.0000 -0.0016 -2.0000
Given what you're doing the pi function may be of interest to you, as may the cospi and sinpi functions.

More Answers (1)

Walter Roberson
Walter Roberson on 4 Feb 2023
syms t
f(t)=[-cos(t), -sin(t), 2*cos(t)^3 - 7*cos(t)*sin(t)^2]
f(t) = 
Then
output = feval(f,3.14) %must "use" the output
output = 
or
f(3.14) %do not need to "use" the output
ans = 
or
subs(f(t), t, 3.14) %do not need to "use" the output
ans = 

Tags

Community Treasure Hunt

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

Start Hunting!