필터 지우기
필터 지우기

Whats wrong with my code?

조회 수: 1 (최근 30일)
Jesse Crotts
Jesse Crotts 2017년 3월 24일
답변: Star Strider 2017년 3월 24일
All I wanna do is plot a user input function. What's wrong with this? I'm new to function handles. So any extra tips would be great too!!!
if true
% code
F = input('Enter a function in terms of x: y= ', 's');
disp('Your function is');
disp(F);
fh = @(x)str2func(F);
d1 = input('Lower domain limit: ');
d2 = input('Upper domain limit: ');
x = linspace(d1-(d2-d1)*.15,d2+(d2-d1)*.15,1000); %15 percent out of domain
y = fh(x);
plot(x,y);
end

채택된 답변

Star Strider
Star Strider 2017년 3월 24일
Your ‘fh’ assignment is incorrect.
Either of these will work:
fh = str2func(vectorize(sprintf('@(x) %s',F)));
fh = str2func(vectorize(['@(x) ',F]));
I prefer the sprintf version. Both are correct.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Modeling에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by