Question

In: Mechanical Engineering

this is my matlab code for class, my professor commented "why is the eps an input...

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

Solutions

Expert Solution

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)


Related Solutions

Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your source code and command widow outcomes and screen shots of any plots in your solution. Develop three functions for temperature-conversion. Create a function called F_to_K that converts and return temperatures in Fahrenheit to Kelvin and store results in ‘F_to_K2.txt’. Create a function called C_to_R that converts and return temperatures in Celsius to Rankine and store results in ‘C_to_R2.txt’. Create a function called C_to_F that...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your source code and command widow outcomes and screen shots of any plots in your solution. Write a user defined function ‘My_FunctionGen’. It accepts, the time vector ‘t’ with 8000 uniformly spaced values within the range of 0 to 8, Frequecy scalars ‘f1<100H’ and ‘f2<80Hz’ and Amplitude scalars ‘A1’, ‘A2’ and ‘A3’ as input arguments. It delivers x1, x2 and x3 and x4 as outputs...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your source code and command widow outcomes and screen shots of any plots in your solution. Generate the following vectors and also implement the Table-1 operations-map by employing the switch statement and an appropriate loop. Generate a vector ‘DEC1’ as follow. DEC1 = 5     1     3     0     2    5     0     2 Concatenate vector ‘DEC1’ eight times in order to obtain a bigger row vector ‘DEC’...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your source code and command widow outcomes and screen shots of any plots in your solution. Develop three functions for temperature-conversion. Create a function called F_to_K that converts and return temperatures in Fahrenheit to Kelvin and store results in ‘F_to_K2.txt’. Create a function called C_to_R that converts and return temperatures in Celsius to Rankine and store results in ‘C_to_R2.txt’. Create a function called C_to_F that...
All Tasks are commented in the code: public class MandelbrotUtil { private MandelbrotUtil() { } /**...
All Tasks are commented in the code: public class MandelbrotUtil { private MandelbrotUtil() { } /** * Return the number of iterations needed to determine if z(n + 1) = z(n) * z(n) + c * remains bounded where z(0) = 0 + 0i. z(n + 1) is bounded if its magnitude * is less than or equal to 2. Returns 1 if the magnitude of c * is greater than 2. Returns max if the magnitude * of z(n...
Please finish this code and make it work. This is my homework and my professor wont...
Please finish this code and make it work. This is my homework and my professor wont allow me to change the code in main, it's a set of huge numbers need to sort by radixsort with bit operation. #include <iostream> using namespace std; void radixLSD_help(int *items, int length, int bit) {    // – Count number of items for each bucket.    // – Figure out where each bucket should be stored (positions // of the first and last element...
Why does my code print nothing in cout? I think my class functions are incorrect but...
Why does my code print nothing in cout? I think my class functions are incorrect but I don't see why. bigint.h: #include <string> #include <vector> class BigInt { private:    int m_Input, m_Temp;    std::string m_String = "";    std::vector<int> m_BigInt; public:    BigInt(std::string s)   // convert string to BigInt    {        m_Input = std::stoi(s);        while (m_Input != 0)        {            m_Temp = m_Input % 10;            m_BigInt.push_back(m_Temp);       ...
Validating Input file, So for my C++ assignment, my professor says we need to validate the...
Validating Input file, So for my C++ assignment, my professor says we need to validate the Input File, Which means, if the line is missing a comma, one of the 3 information, or the string is a white line, it will ignore it. If the line has a white line, the program will correct it and will read in the line. I will store these into an Array, and display them in First name, Last name, and Number of Votes...
Checking Input File for correct line format. So for my C++ assignment, my professor says we...
Checking Input File for correct line format. So for my C++ assignment, my professor says we need to validate the Input File, if there are white lines and spaces, in which it will ignore them, separated by a comma, and that all data items are there. This will be used in a program where i will store these into an Array, and display them in First name, Last name, and Number of Votes. This is a sample of the txt...
Checking Input File for correct line format. So for my C++ assignment, my professor says we...
Checking Input File for correct line format. So for my C++ assignment, my professor says we need to validate the Input File, if there are white lines and spaces, in which it will ignore them, separated by a comma, and that all data items are there. This will be used in a program where i will store these into an Array, and display them in First name, Last name, and Number of Votes vertical columned categories. This is a sample...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT