Login
Order Now
Support
Matlab Assignment Solution on User-defined Functions

Matlab Assignment Solution on User-defined Functions

  • 22nd Jul, 2022
  • 16:05 PM
% the_number_pi.m
%
% FirstName LastName, CSU_ID, Assignment 1
% ESC 152 – Project No. 3 
% Problem 2 
%
% Series approximation of PI
%
% User has to decide whether he wants N approach (give screen input as 0)
% N approach refers to number of seriers terms
% or
% Ns approach (give screen input as 1) - Significant error approach
% if user selects N approach by entering zero
  % user is asked next to enter value of N
  % pi value is approx. by considering series with number of terms as N
% if user selects Ns approach by entering one
  % user is asked next to enter value of Ns
  % pi value is approx. by considering series terms till relative error
  % condition is satisfied.
% The code output on command window, the value of approximated pi, exact
% value of pi and the error.
%
clc;
clear;
display ('FirstName LastName, CSU_ID');
display ('Problem 2 - Assignment Enter_Number_Here');
fprintf('\n')
% save exact value 
%   for comparison purposes
exact = pi;
value = input ('Enter 0 for N or 1 for Ns      : ');
if value==0 % series apparoach
N = input ('Enter number of terms, N      : ');

% initialize
approximation = 0;

for j = 0: 1: N
    term = (-1/3)^j / (2*j + 1);
    
    approximation = approximation + sqrt(12) * term;
    
    Error = abs(exact - approximation);
    
end

% Output

format long 
%display ([j,approximation, Error]); 

% (a, 5 points) 
% Modify the output lines to provide a table with headers.
% Format print 
fprintf(' \n --------------------------------------------------------- \n')
fprintf(' Number of terms      PI value (approx.)           Error \n')
fprintf(' ------------------------------------------------------------ \n')
fprintf('}           .15f   .15f \n',N,approximation,Error)

end

if value==1 % significant figure approach
% (b, 25 points)
?ommon approach is to program this approximation such as it takes 
% as many terms it may need to meet a given criteria.
% 
% One such criteria is for the approximation to reach a given tolerance, 
?fined as (i.e., a measure of the relative change):
% 
%           Relative Change < tolerance> %               where the tolerance can be defined isn several ways
%           [see assignment for more detailed formulas]
%  
?apt the script [using a control structure, “if-then-else”] to include 
% a “user-defined” tolerance (instead of N) as a conditional stopping criteria 
% (or “control structure”). 
% 
% Test your script for different tolerances as specified in your assignment
Ns = input ('Enter tolerance value      : ');
approximation_prev = 0; % Initialization
approximation = 0;

    for j = 0: 1: 1000
    term = (-1/3)^j / (2*j + 1);
    approximation = approximation + sqrt(12) * term;
    % Relative error
    rel_error = abs(approximation-approximation_prev)/( abs(approximation)+10^(-5+Ns) );
    % Break condition if relative error reduces to significant figures
    if rel_error < 0>         break
    end
    approximation_prev = approximation;
    end
    
% Format print    
fprintf(' \n --------------------------------------------------------- \n')
fprintf(' Ns            PI value (approx.)         Error \n')
fprintf(' ------------------------------------------------------------ \n')
fprintf('-           .15f   .15f \n',Ns,approximation,abs(exact-approximation))
end

%

Share this post

assignment helpassignment helperassignment expertsassignment writing services