How do I evaluate a user-input trigonometric function?
조회 수: 3 (최근 30일)
이전 댓글 표시
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
채택된 답변
Birdman
2017년 11월 21일
n=input('enter a pos num\n');
x=linspace(0,n,1000);
trigfunc=input('Enter 1=sin(x), 2=cos(x), 3=tan(x), 4=cot(x)\n');
if(trigfunc==1)
y=sin(x);
plot(x,y);title(['sin(x) from [0 ' ,num2str(n), ']'])
elseif(trigfunc==2)
y=cos(x);
plot(x,y);title(['cos(x) from [0 ' ,num2str(n), ']'])
elseif(trigfunc==3)
y=tan(x);
plot(x,y);title(['tan(x) from [0 ' ,num2str(n), ']'])
elseif(trigfunc==4)
y=cot(x);
plot(x,y);title(['cot(x) from [0 ' ,num2str(n), ']'])
end
댓글 수: 0
추가 답변 (2개)
Prathap
2022년 12월 26일
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
Stephen23
2022년 12월 26일
Much Better Hint: Use STR2FUNC() function within the script to accept different trigonometric functions:
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!