In: Computer Science
determine whether the given function is even, odd, or neither.
Please write a code in MatLab to solve this problem below:
1.f(x) = sin 3x
please only use Matlab to solve this problem
The MATLAB code to calculate the function f(x) = sin(3x) is:
% Creating a flag variable to control while loop
flag=0;
% Using while loop to repeat process untill user wish to exit
% Making the loop to exit program to end when user enter 111
while flag~=111
% Taking value of x from user
x=input('Enter a value for x: ');
% Checking if x is not 111
% If x is 111 we should exit program
if x~=111
% Calculating sin(3x) using sin() built in function
f=sin(3*x);
% Printing output
fprintf("sin(%d) = %.2f\n",3*x,f);
fprintf("Enter 111 to exit\n");
else
fprintf("Good Bye!");
flag=111;
end
end
I have made this code to take the value of x from user to calculate the value of function instead of assigning a range of values to x. If you wish the code to print the value of f(x) for a specified range of x, do let me know the range in the comment section and I will modify the program. This code repeatedly takes values of x and prints the value of f(x) till user enter 111 as input. When user is done with the program he has to enter 111 and the code will terminate by printing a "Good Bye" message.
I have provided comments in the program explaining the lines of code. I have tested the code and validated outputs. I am sharing a output screenshot for your reference.
Hope this answe helps you.
Thank you :)