(Solved) : Create User Defined Function Computes Sin X User Must Enter Choice 1 2 1 Solution Done Us Q31963861 . . .
Create a user-defined function that computes a sin(x). The usermust enter the choice between 1 and 2. If 1, the solution will bedone using for loop. If 2, the solution will be done using whileloop. Answer must be in the form: The sin of ___ is ___ using___loop, which provides an error equal to ____.
This is done with matlab
I’m not sure how to set up the function.
The for loop is:
clcclearm = input(‘Insert number of steps: ‘);x = input(‘Insert value of angle in degrees: ‘);rad_x = x*pi/180;sum_o = 0; %old sumsum_n = 0; %new sumn = 0;error = 1;for i = 0:m term = ((-1)^n) * (rad_x^(2*n+1))/factorial(2*n+1); sum_o = sum_n; sum_n = sum_n+term; n = n+1; if sum_o ~= 0 error = abs((sum_n-sum_o)/sum_o); endendfprintf(“The sin of %.0f is %.2e after %0.f iterations, providing an error equal to %5.2en.”, x, sum_n, m, error)
The while loop is:
clcclearx = input(‘Type value of angle in degrees: ‘);rad_x = x*pi/180;sum_o = 0; %old sumsum_n = 0; %new sumn = 0;goal = 1E-8;error = 1;while error >= goal term = ((-1)^n) * (rad_x^(2*n+1))/factorial(2*n+1); sum_o = sum_n; sum_n = sum_n+term; n = n+1; if sum_o ~= 0 error = abs((sum_n-sum_o)/sum_o); endendfprintf(“The sin of %.0f is %.2e, providing an error equal to %5.2e.n”, x, sum_n, error)
Expert Answer
Answer to Create User Defined Function Computes Sin X User Must Enter Choice 1 2 1 Solution Done Us Q31963861 . . .
OR