Why does Matlab say my function is undefined?
2 次查看(过去 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!