In: Mechanical Engineering
this is my matlab code for class, my professor commented "why is the eps an input when it is set inside the function and not specified as a variable?
how do i fix?
function[] = ()
%Declare Global Variables
global KS;
global KC;
KC = 0;
KS = 0;
End = 0;
while (End == 0)
choice = questdlg('Choose a function', ...
'Fuction Menu', ...
'A','B','B');
switch choice;
case 'A'
Program = 'Start';
while strcmp(Program,'Start');
Choice = menu('Enter the Trigonometric Function you wish to compute', 'Sin', 'Cos','End');
switch Choice;
case 1 %sine
x = input('Input an angle (in Radians):\n'); %x = theta
eps = input('Input a value for epsilon:\n'); %eps=tolerance
[sum] = sine(x,eps);
fprintf('Matlab answer: %5.2f\n',sin(x))
fprintf('Taylor answer: %5.2f\n',sum)
fprintf('KS = %d\n',KS)
fprintf('KC = %d\n',KC)
case 2 %cosine
x = input('Input an angle (in Radians):\n'); %x = theta
eps = input('Input a value for epsilon:\n'); %eps=tolerance
[sum] = cosine(x,eps);
fprintf('Matlab answer: %5.2f\n',cos(x))
fprintf('Taylor answer: %5.2f\n',sum)
fprintf('KS = %d\n',KS)
fprintf('KC = %d\n',KC)
case 3
Program = 'End';
end
end
case 'B'
% Triangle 1 Given Values
A = 1.0472;
b = 7;
C = 0.5236;
%Finding sides a and c and angle B using Law of Sines
B = pi - (A+C);
c = sine (C) / (sine(B)/b);
a = sine (A) / (sine(B)/b);
disp(['Triangle 1'])
fprintf('angle B in radians = %f\n', B);
fprintf('length of side a = %f\n', a);
fprintf('length of side c = %f\n', c);
%Triangle 2 Given Values using Law of Sines and C osines
a = 3;
B = 1.5708;
c = 4;
%Finding side b and angles A and C
b = sqrt((a^2)+(c^2)-(2*a*c*cosine(B)));
C = asin(c * (sine(B)/b));
A = asin(a * (sine(B)/b));
disp(['Triangle 2'])
fprintf('angle of C (in radians) = %f2\n', C);
fprintf('length of side b = %f2\n', b);
fprintf('angle A (in radians) = %f2\n', A);
fprintf('KS = %d\n',KS)
fprintf('KC = %d\n',KC)
end
End = input('Enter 0 to continue or other # to stop: ');
end
%Sine function
function[sum]= sine (x,eps)
KS = KS + 1;
eps = 0.0001;
sum=0;
i=0;
k=1;
while abs(k)>eps
%Taylor Series of Sine
k = (((-1).^i)*((x).^(2*i +1)))/(factorial((2*i)+1));
i = i + 1;
sum= sum+k;
end
end
%Cosine function
function [sum] = cosine (x,eps)
KC =KC + 1;
eps=0.0001;
sum=0;
i=0;
k=1;
while abs(k)>eps
%Taylor Series of Cosine
k = (((-1).^i)*((x).^(2*i)))/(factorial((2*i)));
i = i + 1;
sum= sum+k;
end
end
end
eps = input('Input a value for epsilon:\n'); %eps=tolerance
is the input statement in your program. After reading the input you are passing 'eps' to the functions SIN and COS.
But the value is again initialized in the functions
%Sine function
function[sum]= sine (x,eps)
KS = KS + 1;
eps = 0.0001;
sum=0;
i=0;
k=1;
while abs(k)>eps
%Taylor Series of Sine
k = (((-1).^i)*((x).^(2*i +1)))/(factorial((2*i)+1));
i = i + 1;
sum= sum+k;
end
end
So there is no need to re initialize the value of eps inside the sine and cos function.
NOTE: If you are defining a constant eps inside the function then posibility of error due to input value(i.e. different value) is less. so you can avoid either taking input or defining as a constant (Choose only one method)