Please include a SCREENSHOT OF THE MATLAB to show the coding inputs if possible.
ID: 3817544 • Letter: P
Question
Please include a SCREENSHOT OF THE MATLAB to show the coding inputs if possible.
In a script file named LASTNAME_LAB10_TASK1.m, write a program according to the specifications outlined below. Before starting, clear all variables in the MATLAB Workspace and the contents o MATLAB Command Window. Unless specified, suppress all MATLAB Command Window output. Upon completion, the result of executing your program should look similar to the output shown below. >> LASTNAME_LAB10_TASK1 Enter a value for x (in radians): 2 Enter a threshold in the range (0, 1): -1 Invalid threshold, please ! Enter a threshold value in the range (0, 1): 0 Invalid threshold, please ! Enter a threshold value in the range (0, 1): 1.3 Invalid threshold, please ! Enter a threshold value in the range (0, 1): 1e-5 cos(2, 000000) = -0.4161468365 cosApprox(2.000000) = -0. 4161552028 Number of terms = 6 >LASTNAME_LAB10_TASK1 Enter a value for x (in radians): pi/2 Enter a threshold in the range (0, 1): 1e-4 cos(1.570796) = 0.0000000000 cosApprox(1.570796) = 0.0000247373 Number of terms = 5 >> LASTNAME_LAB10_TASK1 Enter a value for x (in radians): 0.0 Enter a threshold in the range (0, 1): 1e - 7 cos(0.000000) = 1.0000000000 cosApprox(0.000000) = 1.0000000000 Number of terms = 1 Create a scalar variable named x and assign to it a value obtained by prompting with the prompt string 'Enter a value for x (in radians): '. (b) Create a scalar variable named threshold and assign to it a value obtained by prompting the user with the prompt string. "Enter a threshold in the range (0, 1): '. Use a while-end statement to continuously prompt the user until the user enters a valid value for the threshold. You may assume the user will always enter a numerical value. (c) Using another while-end statement to perform the following e value for cos(x) using the summation formula given below. cos Approx(x) almostequalto + x^0/0! - x^2/2! + x^4/4! - x^6/6! + For the value of x entered by the user, accumulate terms of the formula for cosApprox(x) until the absolute value of the difference between the summation of cosApprox(x) and the actual value of cos(x) is less than or equal to the threshold. Compute how many terms were accumulated to achieve the accuracy as determined by the threshold value entered by the user. Use multiple instances of the built-in fprintf() function to display to the Command Window the value of cos(x), the value of cosApprox(x), and the number of terms used for the accumulation. Format the values of cos(x) and cosApprox(x) as fixed-point real numbers showing a maximum of 10 digits after the decimal point. Format the number of accumulated terms as an integer. Thoroughly test your program with more than those input sequences shown in the sample output above.Explanation / Answer
please refer below code
close all
clc
clear all
prompt = 'Enter value for x (in radian): ';
x = input(prompt);
threshold = 0;
while(1)
prompt = ' Enter a threshold in the range (0,1): ';
threshold = input(prompt);
if threshold > 0 && threshold < 1
break;
end
fprintf(' Invalid threshold. Please try again!');
end
exact_val = cos(x); %value computed by matlab
sum = 0;
k = 1;
%calculating value using series given
while abs(sum - exact_val) >= threshold
initial = (-1)^(k-1);
numerator = x^(2*(k-1));
denominator = factorial(2*(k-1));
total=(initial*numerator)/denominator;
sum=sum+total;
k = k + 1;
end
fprintf('cos(%f) = %.10f ', x,exact_val);
fprintf('cosApprox(%f) = %.10f ',x,sum);
fprintf('Number of terms = %d ', k-1);
please refer below output
Enter value for x (in radian): 2
Enter a threshold in the range (0,1): -1
Invalid threshold. Please try again!
Enter a threshold in the range (0,1): 0
Invalid threshold. Please try again!
Enter a threshold in the range (0,1): 1.3
Invalid threshold. Please try again!
Enter a threshold in the range (0,1): 1e-5
cos(2.000000) = -0.4161468365
cosApprox(2.000000) = -0.4161552028
Number of terms = 6
>>
--------------------------------------------------------------------------------------
Enter value for x (in radian): pi/2
Enter a threshold in the range (0,1): 1e-4
cos(1.570796) = 0.0000000000
cosApprox(1.570796) = 0.0000000000
Number of terms = 0
>>
-----------------------------------------------------------------------------------------
Enter value for x (in radian): 0.0
Enter a threshold in the range (0,1): 1e-7
cos(0.000000) = 1.0000000000
cosApprox(0.000000) = 1.0000000000
Number of terms = 1
>>