Why does Matlab say my function is undefined?
조회 수: 3 (최근 30일)
이전 댓글 표시
So I have the following code
x=input('Enter the value x from the interval pi/6 to pi/2:');
while x<pi/6 || x>pi/2
disp(fprintf('Please enter an x value from the specified interval of pi/6 to pi/2 \n'))
x=input('Enter the value x from the interval pi/6 to pi/2:');
if x>pi/6 || x<pi/2
continue
end
end
n=input('Enter an integer for two, three, or four terms:');
while n~=2 || n~=3 || n~=4
disp(fprintf('You already entered that number or you entered an incorrect choice.\n'))
n=input('Enter an integer for two, three, or four terms:');
if n==2
y=func2(x);
Y=fprcall(x,n,y)
elseif n==3
y=func3(x);
Y=fprcall(x,n,y)
elseif n==4
y=func4(x);
Y=fprcall(x,n,y)
else
disp(fprintf('You already entered that number or you entered an incorrect choice.\n'))
end
end
function y=func2(x)
y=x-((x^3)/(factorial(3)));
end
function y=func3(x)
y=x-((x^3)/(factorial(3)))+((x^5)/(factorial(5)));
end
function y=func4(x)
y=x-((x^3)/(factorial(3)))+((x^5)/(factorial(5)))-((x^7)/(factorial(7)));
end
function Y=fprcall(x,n,y)
z=sin(x);
RE= (100*(y-sin(x)))/sin(x);
fprintf('For x=%0.4f and n=%f the values are y=%0.6f, sin(x)=%0.6f, and RE=%0.4f\n',x,n,y,z,RE)
end
For some reason when I run it for lets say when n=3, the function I have will not run. This occurs for all of them. Is there anything I am missing?
댓글 수: 0
답변 (1개)
Kelly Kearney
2018년 11월 27일
If you strip out most of the commands, the main structure of your code looks like this:
n=input('Enter an integer for two, three, or four terms:');
while n~=2 || n~=3 || n~=4
% do stuff
end
So, you ask your user to enter 2, 3, or 4... but then only execute your commands if the user starts by entering an incorrect choice.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!