Question

Problem 5 xx The Taylor series expansion for sin(x) is sin(x) = x -H + E-E+ = o E- (-1) . 2n +1 no (2n+1)! !57 where x is in radians. Write a MATLAB program that determines sin(x) using the Taylor series expansion. The program asks the user to type a value for an angle in degrees. Then the program uses a while loop for adding the terms of the Taylor series. If an n is the nth term in the series, then the sum Sne of the n terms is S, =Sau + E. In each pass calculate the estimated error E given by E = sner said. Stop adding terms when E 1E-8. The program displays the - Sold value of sin(s). Use the program for calculating sin(45) and sin(195) Show the results as: The sing of L is providing an error equal to L. Use scientific notation with 2 decimal places Problem 6 Write a MATLAB program for problem 5 using a for loop. Run the program for 5, 10, and 15 steps. Display the error at the end of the calculations.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Matlab Code:

Problem 5:

% MATLAB Program that calculates Taylor series expansion of sin(x)

clc;
clear;

% Reading angle in degrees
angleDegrees = input("Enter angle in degress: ");

% Converting angle in to radians
angleRadians = (0.0174533) * angleDegrees;

% Initially assign value 1
EstError = 1;

% Variable n for series
n = 0;

% Assign 0 to series sum
SNew = 0;
SOld = 0;

% Iterate till E > 1E-8
while EstError > (1e-8)

    % Calculating term
    term = ( ((-1)^n) / (factorial((2*n) + 1)) ) * ( angleRadians ^ ((2*n) + 1) );

    % Adding term
    if n == 0
      SOld = term; % Updating SOld
      EstError = abs((SNew - SOld)/SOld); % Calculating E
    else     
      SNew = SOld + term; % CAdding term to series
      EstError = abs((SNew - SOld)/SOld); % Calculating E
      SOld = SNew; % Updating SOld value
    end
  
    % Updating n value  
    n = n + 1;

end % End of while

% Printing resultant values
fprintf("\n The sin of %.2f is %.8f, providing an error equal to %.2e \n", angleDegrees, SOld, EstError);

Sample Run:

Enter angle in degress: 45 The sin of 45.00 is 0.70710702, providing an error equal to 2.49e-009 7

________________________________________________________________________________________________

Problem 6:

% MATLAB Program that calculates Taylor series expansion of sin(x)

clc;
clear;

% Reading angle in degrees
angleDegrees = input("Enter angle in degress: ");

% Converting angle in to radians
angleRadians = (0.0174533) * angleDegrees;

% Initially assign value 1
EstError = 1;

% Assign 0 to series sum
SNew = 0;
SOld = 0;

% Iterate for loop from 1 to 15
for n=0:14

    % Calculating term
    term = ( ((-1)^n) / (factorial((2*n) + 1)) ) * ( angleRadians ^ ((2*n) + 1) );

    % Adding term
    if n == 0
      SOld = term; % Updating SOld
      EstError = abs((SNew - SOld)/SOld); % Calculating E
    else     
      SNew = SOld + term; % CAdding term to series
      EstError = abs((SNew - SOld)/SOld); % Calculating E
      SOld = SNew; % Updating SOld value
    end
  
end % End of while

% Printing resultant values
fprintf("\n The sin of %.2f is %.8f, providing an error equal to %.2e \n", angleDegrees, SOld, EstError);

Sample Run:

_________________________________________________________________________________________

Problem 8:

function result = sinSeries_8(angleDegrees)
    #MATLAB function that reads angle in degrees and then calculates and returns value of sin(x)
  
    % Reading user choice
    ch = input("\n 1 - For Loop \t 2 - While Loop \t Your Choice: ");

    % Converting angle in to radians
    angleRadians = (0.0174533) * angleDegrees;

    % Initially assign value 1
    EstError = 1;

    % Variable n for series
    n = 0;

    % Assign 0 to series sum
    SNew = 0;
    SOld = 0;

    % Using for loop
    if ch == 1
      
        % Iterate for loop from 1 to 15
        for n=0:14
        
            % Calculating term
            term = ( ((-1)^n) / (factorial((2*n) + 1)) ) * ( angleRadians ^ ((2*n) + 1) );
        
            % Adding term
            if n == 0
              SOld = term; % Updating SOld
              EstError = abs((SNew - SOld)/SOld); % Calculating E
            else     
              SNew = SOld + term; % CAdding term to series
              EstError = abs((SNew - SOld)/SOld); % Calculating E
              SOld = SNew; % Updating SOld value
            end
          
        end % End of while

        % Printing resultant values
        fprintf("\n The sin of %.2f is %.8f using for loop, which provides an error equal to %.2e \n", angleDegrees, SOld, EstError);

      
    % Using while loop
    else
  
        % Iterate till E > 1E-8
        while EstError > (1e-8)
        
            % Calculating term
            term = ( ((-1)^n) / (factorial((2*n) + 1)) ) * ( angleRadians ^ ((2*n) + 1) );
        
            % Adding term
            if n == 0
              SOld = term; % Updating SOld
              EstError = abs((SNew - SOld)/SOld); % Calculating E
            else     
              SNew = SOld + term; % CAdding term to series
              EstError = abs((SNew - SOld)/SOld); % Calculating E
              SOld = SNew; % Updating SOld value
            end
          
            % Updating n value  
            n = n + 1;

        end % End of while
      
        % Printing resultant values
        fprintf("\n The sin of %.2f is %.8f using while loop, which provides an error equal to %.2e \n", angleDegrees, SOld, EstError);
      
    end
  
end

Sample Output:

Command Window >sinSeries_8 (45) 1 For Loop 2 While Loop The sin of 45.00 is 0.70710702 using for loop, which provides an err

Add a comment
Know the answer?
Add Answer to:
Problem 5 xx The Taylor series expansion for sin(x) is sin(x) = x -H + E-E+...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT