Question: Create a user-defined function that computes a sin(x). The user must enter the choice between 1 and 2. If 1, the solution will be done
Create a user-defined function that computes a sin(x). The user must enter the choice between 1 and 2. If 1, the solution will be done using for loop. If 2, the solution will be done using while loop. 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:
clc clear m = input('Insert number of steps: '); x = input('Insert value of angle in degrees: '); rad_x = x*pi/180; sum_o = 0; %old sum sum_n = 0; %new sum n = 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); end end fprintf("The sin of %.0f is %.2e after %0.f iterations, providing an error equal to %5.2e .", x, sum_n, m, error) The while loop is:
clc clear x = input('Type value of angle in degrees: '); rad_x = x*pi/180; sum_o = 0; %old sum sum_n = 0; %new sum n = 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); end end fprintf("The sin of %.0f is %.2e, providing an error equal to %5.2e. ", x, sum_n, error) Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
