Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PART 1 (I already solved part 1 but it's necessary for parts 2 and 3) The following are infinite series representations of the functions sine,

PART 1 (I already solved part 1 but it's necessary for parts 2 and 3)

The following are infinite series representations of the functions sine, cosine, and the exponential.

sin(??) = ?(?1)^?? (??^(2??+1))/(2?? + 1)! (? on top of the sigma)(??=0 on the bottom of the sigma)

cos(??) = ?(?1)^?? (??^(2??))/(2??)! (? on top of the sigma)(??=0 on the bottom of the sigma)

??^?? = ? ??^??/??! (? on top of the sigma)(??=0 on the bottom of the sigma)

Performing the summation only to N terms is called a partial sum and is denoted by SN.

???? = ???n (?? on top of the sigma)(??=0 on the bottom of the sigma)

Within your script file, use nested for loops to compute the first 5 partial sums with N = 1, 2, 3, 4 and 5 in order to estimate the following:

a) sin(?/3)

b) cos(?/4)

c) ??^?2

Assuming that the built-in MATLAB functions sin(), cos(), and exp()return the exact values, calculate the absolute difference between the value of the function determined by your code and the MATLAB function. Print out your results, and ONLY your results, to the command window as seen below where S(N) is the partial sum out to N values.

PART a) sin(pi/3) -> MUST INCLUDE THIS LINE IN YOUR OUTPUT

S(1) = 0.8558007816, Error = 1.022462221932130E-02

S(2) = 0.8662952838, Error = 2.698800023961390E-04

S(-) = ------------, Error = ---------------------

S(-) = ------------, Error = ---------------------

HINT: Use fprintf inside the outside loop with format i for N (1, 2, , 5), format .10f for the value of SN, and format.15E for the absolute error. You can use the MATLAB function factorial() to determine the factorial; however, DO NOT use the sum() function in MATLAB.

PART 2

Using the functions mentioned in HW6P2. Prompt the user for the name of a function (sin, cos, or exp). If the user enters something else other than the exact function name, the program must display an error message and re-prompt the user to enter one of the three functions until the user enters a valid entry.

The program should then prompt the user to enter a value of x for the corresponding function, i.e. function = sin, x = pi/3 means that sin(pi/3) will be evaluated.

Lastly, the program should prompt the user to enter the number of terms to be included in the partial sum, N, as an integer >0. If the user enters an invalid number, the program must display an error message and re-prompt the user to enter in the number of terms.

HINT: Use a while loop for valid entry. Use the strcmp() function for comparison between strings and the rem() function to determine if a number is an integer.

Your program must use a branching structure if-else or switch-case to execute only the function entered by the user and determine its value at x entered. Use a for loop to compute the partial sum SN for the specified N entered by the user.

The program then must print the result to the command window as seen below.

function(x) out to N terms = x.xxxxxx

Example output where function = sin, x = pi/6, and N = 5:

sin(0.523599) out to 5 terms = -0.500000

You may test your script for the following cases with N = 5, 10, and 20:

i. sin(0), sin(???/6), sin(??/4), sin(??/3), sin(??/2), sin(3??/2).

ii. cos(0), cos(???/6), cos(??/4), cos(??/3), cos(??/2), cos(3??/2).

iii. exp(0) , exp(?2) , exp(3) , exp(?5) , exp(3.5)

iv. SIN(0), COS(???/6), EXP (3)

v. sin(cos ( ??/6 ) , cos ( ??/3 ) , exp(2.5) with ?? = 0 and ? 10

PART 3

An alternative way of working the previous problem is to use a while loop instead of a for loop. In this case, the number of terms N is not predefined or entered by the user, instead the user enters the precision of calculation in the form of a very small number, i.e. 10-6 or 10-12, and the loop must continue until this precision is reached. The nature of the previous series which is calculated by the sum from 0 to ? is that all sums approach an approximately constant value as ?? ? ?. In other words, eventually, the difference between the value of the function for N terms and that for ?? + 1 terms becomes very small and insignificant.

Copy and paste your code from HW6P3 to the end of your script file. Modify your code to calculate each function using a while loop. The stopping criterion for your loop must be either for the nth term to become < precision or the absolute difference (???? ? ????+1) becomes < precision. Within your loop, determine the number of iterations executed until the stopping criterion was reached.

Instead of prompting the user to enter the number of terms, N, the program must prompt the user to enter the precision in E format for example 10-8 will be entered 1E-8. The precision must be a positive number and at least < 1E-1. If the user enters an invalid number, the program must display an error message and re-prompt the user to enter in the precision.

The program then must print the result to the command window as seen below.

function(x) with a precision of xE-xx = x.xxxxxx after N terms

Example output where function = sin, x = pi/6, and precision of 1E-12:

sin(0.523599) with a precision of 1E-12 = 0.500000 after 7 terms

You may test your script for the following cases with a precision of 1E-5, 1E-10, and 1E-15:

i. sin(0), sin(???/6), sin(??/4), sin(??/3), sin(??/2), sin(3??/2).

ii. cos(0), cos(???/6), cos(??/4), cos(??/3), cos(??/2), cos(3??/2).

iii. exp(0) , exp(?2) , exp(3) , exp(?5) , exp(3.5)

iv. SIN(0), COS(???/6), EXP (3)

v. sin(cos ( ?? 6 ) , cos ( ??/3 ) , exp(2.5) with ?????????????????? = 0 and ?1E-10.

The script file for Part 1 is

N=[1 2 3 4 5]; A=[0 0 0 0 0]; B=A;C=B; x=(pi/3); for i=1:1:5 for j=0:N(i) A(i)=A(i)+(-1)^j*x^(2*j+1)/factorial(2*j+1); end end C=abs(sin(x)-A); fprintf('PART a) sin(pi/3) ') fprintf('S(%i)=%.10f, Error=%.15e. ',[N;B;C]) x=(pi/4); for i=1:1:5 for j=0:N(i) B(i)=B(i)+((-1)^j)*(x^(2*j))/factorial(2*j); end end C=abs(cos(x)-B); fprintf('PART b) sin(pi/4) ') fprintf('S(%i)=%.10f, Error=%.15e. ',[N;B;C]) x=-2; C=[0 0 0 0 0]; for i=1:1:5 for j=0:N(i) C(i)=C(i)+x^j/(factorial(j)); end end e=abs(exp(x)-C); fprintf('PART c) e^-2 ') fprintf('S(%i) = %.10f, Error = %.15e. ',[N;C;e])

I would like the script file in matlab for Parts 2 and 3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Decisions Based On Data Analytics For Business Excellence

Authors: Bastian Weber

1st Edition

9358681683, 978-9358681680

More Books

Students also viewed these Databases questions