Question

In: Computer Science

determine whether the given function is even, odd, or neither. Please write a code in MatLab...

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

Solutions

Expert Solution

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 :)


Related Solutions

Write c code to determine if a binary number is even or odd. If it is...
Write c code to determine if a binary number is even or odd. If it is odd, it outputs 1, and if it is even, it outputs 0. It has to be less than 12 operations. The operations have to be logical, bitwise, and arithmetic.
Use Matlab to write the following 1. Write code to determine whether a year is a...
Use Matlab to write the following 1. Write code to determine whether a year is a leap year. Use the mod function. The rules for determining leap years in the Gregorian calendar are as follows: All years evenly divisible by 400 are leap years. Years evenly divisible by 100, but not by 400, are not leap years. Years divisible by 4, but not by 100, are leap years. All other years are not leap years. For example, the years 1800,...
Develop a C++ function to find the number of even and odd integers in a given...
Develop a C++ function to find the number of even and odd integers in a given array of integers Pass in an array of integers Pass in the size of the array of integers Pass in a reference parameter for the number of even values Pass in a reference parameter for the number of odd values The function doesn't return anything Develop a C++ program to test your function
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The function will have one int 1D array n and one int size as parameters. The function will display all the values of n on one line. Function #2 Write the function AverageEvenOdd. The function will have one int 1D array n and one int size as parameters. The size of the array is given by the parameter int size. Therefore, the function should work...
Write a matlab code for given task Use your ‘sin’ or ‘cos’ function to generate a...
Write a matlab code for given task Use your ‘sin’ or ‘cos’ function to generate a sinusoid wave having two components as f1 = 3kHz and f2 = 5kHz and then sample it with fs = 10kHz. Calculate its fft with zero frequency component in the middle. Plot it on a properly scaled w-axis. Specify if there is aliasing or not? If there is aliasing specify which component is casing the aliasing
Question1. (lesson 2) In the following code we determine a number if its even or odd....
Question1. (lesson 2) In the following code we determine a number if its even or odd. Explain what logic is used for that?    section .text    global _start            ;must be declared for using gcc        _start:                     ;tell linker entry point    mov   ax,   8h           ;getting 8 in the ax    and   ax, 1              ;and ax with 1    jz    evnn    mov   eax, 4             ;system call number (sys_write)    mov   ebx, 1             ;file descriptor (stdout)    mov   ecx, odd_msg       ;message...
Write a function name as 'checkEvenOrOdd' that checks the input value is odd or even number....
Write a function name as 'checkEvenOrOdd' that checks the input value is odd or even number. The main function is given: int main(){     int number;     cout << "Check number input: ";     cin >> number;     cout << "The input number " << number << " is " << checkEvenOrOdd(number) << endl;     return 0; } simple c++ code please
Write and submit at a Matlab function that sums up a specified number of odd-power terms...
Write and submit at a Matlab function that sums up a specified number of odd-power terms from the Maclaurin series of sin(x)  using following function format function s = etaylor(x, n) % Sum of first n non-vanishing terms of Maclaurin series of sin(x) % For example, etaylor(0.5, 1) should be 0.5, and etaylor(0.5, 2) should be 23/48   
Matlab Code Write a procedure to calculate the log discriminant function for a given multi-variate Gaussian...
Matlab Code Write a procedure to calculate the log discriminant function for a given multi-variate Gaussian distribution and prior probability
This code is to be written in Matlab. Write a function that will plot cos(x) for...
This code is to be written in Matlab. Write a function that will plot cos(x) for x values ranging from -pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths. If no arguments are passed to the function, the line widths will be 1, 2, and 3. If on the other hand, an argument is passed to the function, it is multiplier for these values....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT