0% found this document useful (0 votes)
12 views6 pages

HW 3 Results

Uploaded by

carsredmond
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

HW 3 Results

Uploaded by

carsredmond
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1. The integral calculated using the simpson34() function came out to be 1.0000181.

This
was because the ⅜ rule was calculated first. The code used for this result can be found
in the other part of this deliverable, “HW set 3 MATLAB Code”.

2a. fun = @(x) sin(x);


low_lim = 0;
upp_lim = pi/2;
n = 8;
integral = simpson34(fun,low_lim,upp_lim,n);
function I = simpson34(fun,low_lim,upp_lim,n)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FUNCTION NAME: simpson34
%
% PURPOSE: Integrate over an interval using a combination of Simpson's 1/3
% Rule and Simpson's 3/8 Rule depending on the even/odd number of points
%
% INPUT: input function, lower bound, higher bound, number of points
%
% OUTPUT: Area under the curve
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% AUTHOR: Carson Redmond
% DATE: 10/9/2024
%
% DESCRIPTION OF LOCAL VARIABLES: fun: local function to be called,
% low_lim: lower bound of integration, upp_lim: higher bound of
% integration, n: number of points to integrate on
%
% FUNCTIONS CALLED:
%
% START OF EXECUTABLE CODE
%
%% Define important variables
h = (upp_lim-low_lim)/(n-1); % width of interval
x = low_lim:h:upp_lim; % array of x values to integrate over
y = fun(x); % data values of initial function
%% If the number of points is even...
if mod(n,2) == 0 % test if the number of points is even
I = 3/8*h*(y(1)+3*y(2)+3*y(3)+y(4)); % Simpson's 3/8 rule

for i = 4:2:n
I = I + 1/3*h*y(i) ; % include odd terms in 1/3 rule
end
if n > 6
for i = 6:2:n-2
I = I + 1/3*h*y(i) ; % count the intervals between iterations of simpson's
rule
end
end
for i = 5:2:n-1
I = I + 1/3*h*4*y(i); % include even terms in the 1/3 rule
end
end
%% If the number of points is odd...
if mod(n,2) ~= 0 % test if the number of points is odd
I = 1/3*h*(y(1) + 4*y(2) + y(3)); % First 3 points of Simpson's 1/3 rule
for i = 3:2:n
I = I + 1/3*h*y(i); % include first and third term for the 1/3 rule
end
for i = 4:2:n
I = I + 1/3*h*4*y(i); % include the second term for the 1/3 rule
end
if n > 5 % include the iterations between the use of subsequent Simpson's
rules
for i = 5:2:n-2
I = I + 1/3*h*y(i);
end
end
end
end

The above code is the function designed to use Simpson's rule to integrate a function over a
given integral with a given amount of points. The function distinguishes between whether to use
Simpson’s ⅓ rule only or a combination of Simpson’s ⅜ and ⅓ rules depending on the amount
of points.
2b. fun = @(phi) sqrt(1-((9-4)./9).*sind(phi).^2);
low_lim = 0;
upp_lim = 60;
n = 5;
integral = simpson34(fun,low_lim,upp_lim,n);
arclength = 3*integral;
disp('The arc length of this ellipse given that a = 3 and b = 2 is:')
disp(arclength)
disp('Only 5 data points are required to reach within 0.1% of the correct value
for an arc length between 0 and 60 degrees')
function I = simpson34(fun,low_lim,upp_lim,n)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FUNCTION NAME: simpson34
%
% PURPOSE: Integrate over an interval using a combination of Simpson's 1/3
% Rule and Simpson's 3/8 Rule depending on the even/odd number of points
%
% INPUT: input function, lower bound, higher bound, number of points
%
% OUTPUT: Area under the curve
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% AUTHOR: Carson Redmond
% DATE: 10/9/2024
%
% DESCRIPTION OF LOCAL VARIABLES: fun: local function to be called,
% low_lim: lower bound of integration, upp_lim: higher bound of
% integration, n: number of points to integrate on
%
% FUNCTIONS CALLED:
%
% START OF EXECUTABLE CODE
%
%% Define important variables
h = (upp_lim-low_lim)/(n-1); % width of interval
x = low_lim:h:upp_lim; % array of x values to integrate over
y = fun(x); % data values of initial function
%% If the number of points is even...
if mod(n,2) == 0 % test if the number of points is even
I = 3/8*h*(y(1)+3*y(2)+3*y(3)+y(4)); % Simpson's 3/8 rule

for i = 4:2:n
I = I + 1/3*h*y(i) ; % include odd terms in 1/3 rule
end
if n > 6
for i = 6:2:n-2
I = I + 1/3*h*y(i) ; % count the intervals between iterations of simpson's
rule
end
end
for i = 5:2:n-1
I = I + 1/3*h*4*y(i); % include even terms in the 1/3 rule
end
end
%% If the number of points is odd...
if mod(n,2) ~= 0 % test if the number of points is odd
I = 1/3*h*(y(1) + 4*y(2) + y(3)); % First 3 points of Simpson's 1/3 rule
for i = 3:2:n
I = I + 1/3*h*y(i); % include first and third term for the 1/3 rule
end
for i = 4:2:n
I = I + 1/3*h*4*y(i); % include the second term for the 1/3 rule
end
if n > 5 % include the iterations between the use of subsequent Simpson's
rules
for i = 5:2:n-2
I = I + 1/3*h*y(i);
end
end
end
end

This code utilizes the same function described above, except this time using a different formula
to determine the arc length of an ellipse over the interval from 0 to 60 degrees. The resulting
value obtained from using this function over a significant amount of points (over 100) gave a
value of approximately 164.15. This was assumed to be within the actual value to such a degree
that it can be assumed to be the real value to compare to. Then, after iterating this code, it was
found that only using 5 points leads to a value within 0.1% of this real one.
3. fun = @(phi) sqrt(1-((9-4)./9).*sind(phi).^2);
low_lim = 0;
upp_lim = 360;
n = 10;
integral = simpson34(fun,low_lim,upp_lim,n);
arclength = 3*integral;
%disp('The arc length of this ellipse given that a = 3 and b = 2 is:')
%disp(arclength)
%disp('Only 5 data points are required to reach within 0.1% of the correct
value for an arc length between 0 and 60 degrees, for the 0 to 360 interval, it
took 10 points.')
%% Use trapz function to compare accuracy

phi = linspace(low_lim,upp_lim,n);
y = fun(phi);

arclength2 = 3*trapz(phi,y);

disp('at 10 points, the trapz function appeared to have a similar accuracy


however the simpson34 function quickly outpaces this method as more points are
added and margins of relative error get much smaller for the simpson method.')
function I = simpson34(fun,low_lim,upp_lim,n)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FUNCTION NAME: simpson34
%
% PURPOSE: Integrate over an interval using a combination of Simpson's 1/3
% Rule and Simpson's 3/8 Rule depending on the even/odd number of points
%
% INPUT: input function, lower bound, higher bound, number of points
%
% OUTPUT: Area under the curve
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% AUTHOR: Carson Redmond
% DATE: 10/9/2024
%
% DESCRIPTION OF LOCAL VARIABLES: fun: local function to be called,
% low_lim: lower bound of integration, upp_lim: higher bound of
% integration, n: number of points to integrate on
%
% FUNCTIONS CALLED:
%
% START OF EXECUTABLE CODE
%
%% Define important variables
h = (upp_lim-low_lim)/(n-1); % width of interval
x = low_lim:h:upp_lim; % array of x values to integrate over
y = fun(x); % data values of initial function
%% If the number of points is even...
if mod(n,2) == 0 % test if the number of points is even
I = 3/8*h*(y(1)+3*y(2)+3*y(3)+y(4)); % Simpson's 3/8 rule

for i = 4:2:n
I = I + 1/3*h*y(i) ; % include odd terms in 1/3 rule
end
if n > 6
for i = 6:2:n-2
I = I + 1/3*h*y(i) ; % count the intervals between iterations of simpson's
rule
end
end
for i = 5:2:n-1
I = I + 1/3*h*4*y(i); % include even terms in the 1/3 rule
end
end
%% If the number of points is odd...
if mod(n,2) ~= 0 % test if the number of points is odd
I = 1/3*h*(y(1) + 4*y(2) + y(3)); % First 3 points of Simpson's 1/3 rule
for i = 3:2:n
I = I + 1/3*h*y(i); % include first and third term for the 1/3 rule
end
for i = 4:2:n
I = I + 1/3*h*4*y(i); % include the second term for the 1/3 rule
end
if n > 5 % include the iterations between the use of subsequent Simpson's
rules
for i = 5:2:n-2
I = I + 1/3*h*y(i);
end
end
end
end

at 10 points, the trapz() function appeared to have a similar accuracy. However, the simpson34()
function quickly outpaces this method as more points are added and margins of relative error
get much smaller for the simpson method. The conditions for this comparison were the same
ellipse arc formula provided in the problem statement, an a value of 3, and a b value of 2. The
amount of steps tested spanned from 10 to 20.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy