Submission Instructions: Your solutions to this assignment must be typed and nea
ID: 3867078 • Letter: S
Question
Submission Instructions: Your solutions to this assignment must be typed and neatly presented. Unorganized solutions and/or solutions that do not present the required deliverables will not be graded and receive a score of zero.
1. [45 pts] Part 1 - "For" and "While" loops in MATLAB Consider the approximation for sin(x) given in homework #2 as: 3 9 13 15 17 sin x = x 3! 5!7!9! 11 13 15!17! Create a vector where each element is a term of the series. Allow the user to input the value of x (as a scalar). Sum the terms of the series using a "for" loop and create a formatted output to show the user (1) the value of the approximation, (2) the actual value of sin(x) (from the MATLAB function) and (3) the error between the two. Use values x 2, 2.5, 3 to compare. ii.Now consider the generalized form of the Taylor series for sin(x): 2n+1 sinx = (-1)" n=0 Sum the first 100 terms of the series and show the result as a formatted output identical to that of 1(i). Allow the user to input the value of x as a scalar. Repeat this problem with the first 250 terms. Compare the results. (Use same x values as from (i))Explanation / Answer
Answer:
(i) # MatLab program for given sin(x) funtion:
clc;
close all;
clear all;
format long;
x = input('Enter x value: ');
sinx = 0;
k = 2;
for i=1:2:17
sinx = sinx + (-1).^k*(x.^i)/factorial(i);
k = k + 1;
end
fprintf('The value of approximation is: %d ',sinx);
fprintf('The original value of sin(%.2f) is:%d ',x,sin(x));
fprintf('The error is: %d ',sinx-sin(x));
Output:
Enter x value: 2
The value of approximation is: 9.092974e-01
The original value of sin(2.00) is:9.092974e-01
The error is: 4.269252e-12
Enter x value: 2.5
The value of approximation is: 5.984721e-01
The original value of sin(2.50) is:5.984721e-01
The error is: 2.946692e-10
Enter x value: 3
The value of approximation is: 1.411200e-01
The original value of sin(3.00) is:1.411200e-01
The error is: 9.353375e-09
(ii) % matlab code
sumSinx = 0;
x = input("Enter x: ");
n = input("Enter n: ");
for i=0:n
sumSinx = sumSinx + ((-1)^i)*(x^(2*i+1)/(factorial(2*i +1)));
end
fprintf("n: %d x: %f sinx = %0.10f ",n,x,sumSinx);
% end matlab code
%{
Output:
Enter x: 2
Enter n: 100
n: 100 x: 2.000000 sinx = 0.9092974268
Enter x: 2
Enter n: 250
n: 250 x: 2.000000 sinx = 0.9092974268
Enter x: 2.5
Enter n: 250
n: 250 x: 2.500000 sinx = 0.5984721441
Enter x: 3
Enter n: 250
n: 250 x: 3.000000 sinx = 0.1411200081
%}
(iii) # MatLab program
clc;
close all;
clear all;
format long;
x=60;
%conversion of degree to radians
x=x*pi/180;
S1=sin(x);
num=0;
S2=0;
error=100;
%execute Loop until the 1E-4
while error>1E-4
S2=S2+((-1)^num*x^(2*num+1)/factorial(2*num+1));
error=abs((S1-S2)/S1)*100;
num=num+1;
I(num)=num;
E(num)=error;
end
%Display the result
fprintf(' Iteration:%f Number of terms in the series:%f Error is:%f ',I,I,E);
%Plot the graph iteration vs. error
plot(I,E);
Output:
$octave -qf --no-window-system main.m
Iteration: 1.000000
Number of terms in the series: 2.000000
Error is: 3.000000
Iteration: 4.000000
Number of terms in the series: 5.000000
Error is: 1.000000
Iteration: 2.000000
Number of terms in the series: 3.000000
Error is: 4.000000
Iteration: 5.000000
Number of terms in the series: 20.919958
Error is: 1.180638
Iteration: 0.031163
Number of terms in the series: 0.000477
Error is: 0.000005