How do I evaluate a user-input trigonometric function?
Mostrar comentarios más antiguos
I'm not entirely sure how to explain this but here is the question:

My main issue is I don't know how to use eval when using a user-input trigonometric function. (I don't really need help with the graphing part).
This is my attempt:
n = input('Enter a positive number n: ');
x = linspace(0, n, 1000)
trigfunc = input('Enter a trigonometric function: ', 's');
eval('trigfunc(x)')
1 comentario
"Then, by using the function eval..."
Ugh, why teach impressionable students such awful programming practices?
Why not simply use a basic look-up, or str2func() or feval() ?
Respuesta aceptada
Más respuestas (2)
num = str2double(input('End value of vector: ','s'));
fun = str2func(input('Trig function (e.g. sin, cos, etc.): ','s'));
x = linspace(0,num,1000);
y = fun(x);
plot(x,y)
To which it is also easy to add input checking, etc.
Prathap
el 26 de Dic. de 2022
0 votos
Plot the trigonometric curve as a function of time. Hint: Use ‘eval’ function within the script to accept different trigonometric functions (e.g., sin, cos, tan, etc.).
1 comentario
Stephen23
el 26 de Dic. de 2022
Much Better Hint: Use STR2FUNC() function within the script to accept different trigonometric functions:
Categorías
Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!