Assignment3_NoelErika
pdf
keyboard_arrow_up
School
University of Florida *
*We aren’t endorsed by this school
Course
4503
Subject
Mathematics
Date
Apr 3, 2024
Type
Pages
12
Uploaded by erikanoel34
Name: Erika Noel
Date: 09/06/2023
PID: 6152487
Assignment 3 : Quadrature
General Instructions:
Complete each question to the best of your abilities. Include comments in your code to explain your thought process while answering.
Submission Instructions: Assignment files should be titled Assignment_X_FirstNameLastName, Ex: Assignment_3_AsadMirza. To convert the MLX file into a PDF go to the Export icon under Live Editor and select "Export to PDF".
Question 1 (20%): Consider the points {(1,1), (2,-2), (3,0), (4,2), (5,0)}
a) (5%) Calculate a cubic polynomial approximation using polyfit.
x =[1,2,3,4,5]
x = 1×5
1 2 3 4 5
y = [1,-2,0,2,0] y = 1×5
1 -2 0 2 0
C = polyfit(x,y,3)
C = 1×4
-0.7500 6.8929 -18.3571 13.2000
b) (10%) Calculate a polynomial approximation using Lagrange.
L = polyfit(x,y,4)
1
L = 1×5
0.0417 -1.2500 8.9583 -21.7500 15.0000
c) (5%) Plot all approximations in the same plot including the data points.
xs = linspace(1,5);
plot(x,y,
'kx'
,xs,polyval(C,xs),
'b--'
,xs,polyval(L,xs),
'r-.'
,
'LineWidth'
,1,
'M
arkerSize'
,8)
xlabel(
'x'
)
ylabel(
'y'
)
legend(
'Data: (x,y)'
,
'Cubic Polynomial'
,
'Lagrange Polynomial'
,
'Location'
,
'northwest'
)
grid on
title(
'Polynomial approximations of given data'
)
Question 2 (10%):
a) (5%) Generate a set of 5 equidistant points within [-1 1] from the function . Employ MATLAB's spline function to interpolate these points. syms x
;
syms f(x)
;
% define function
f(x)=1/(1+25*x^2);
2
% 5 equidistance point
X=linspace(-1,1,5);
% functional value
% change expression value to double value
F=eval(f(X));
% X values
XVal=linspace(-2,2,100);
% Y values using spline
YVal=spline(X,F,XVal);
b) (5%) Plot the resulting function along with the data points on the same graph.
% plot both data sets
plot(XVal,YVal);
hold on
;
plot(X,F,
'ro'
);
xlabel(
'X values'
);
ylabel(
'Y values'
);
legend(
'Spline Data'
,
'Original Data'
)
Question 3 (20%)
3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Consider the interpolation problem employing the nine data points generated by evaluating the function at the following nine �
values
.
a) (10%) Calculate the 8-degree polynomial interpolation solution using polyfit function.
% Create a vector of x values x = [-5, -3, -1, -1/2, 0, 1/3, 1, 3, 6];
% Create an anonymous function
f = @(x) ((x-2).*(2*x+1))./(1+x.^2);
% Compute y values for each value of x y = f(x);
% Create a vector for more x values from - 5 to 6 xFit = linspace(-5, 6);
% Create first subplot subplot(1, 3, 1)
% Plot the original function plot(xFit, f(xFit));
% Set title title(
'Original Function'
);
a) (5%) Calculate the polynomial interpolation solution using cubic spline function.
% Compute the 8-degree polynomial to the x & y data p = polyfit(x, y, 8);
% Compute the fitted values for xfitted values yFit = polyval(p, xFit);
% Create second subplot subplot(1, 3, 2)
% Plot the 8-degree polynomial
plot(xFit, yFit)
% Set title title(
'8-degree polynomial'
);
c) (5%) Plot all interpolations and the original function.
% Compute the interpolated values for cubic spline ySpline = spline(x, y, xFit);
% Create third subplot 4
subplot(1, 3, 3)
% Plot the cubic spline plot(xFit, ySpline);
% Set title title(
'Cubic Spline'
);
Question 4 (15%)
a) (10%) Using Midpoint Rule, estimate the integral using �
=20.
f = @(x) 6*sin(x).*x.^2; % define the function
a = 0; % lower limit
b = 3; % upper limit
n = 20; % number of subintervals
h = (b-a)/n; % width of each subinterval
x = a+h/2:h:b-h/2; % midpoint of each subinterval
sum_f = sum(f(x)); % sum of function values at midpoints
I = h*sum_f; % Midpoint Rule formula for integral
disp([
'Midpoint Rule estimate: '
, num2str(I)])
Midpoint Rule estimate: 34.7053
5
b) (5%) Compare value with integral function and plot the function.
syms x
I_exact = int(6*sin(x)*x^2, x, 0, 3);
x_vals = 0:0.1:3;
y_vals = f(x_vals);
plot(x_vals, y_vals)
xlabel(
'x'
)
ylabel(
'y'
)
title(
'Graph of f(x)'
)
x_vals = 0:0.1:3;
y_vals = f(x_vals);
plot(x_vals, y_vals)
hold on
plot([3 3], [0 f(3)], '--r'
)
hold off
I_exact = double(I_exact);
disp([
'Exact integral: '
, num2str(I_exact)])
Exact integral: 34.66
disp([
'Midpoint rule estimate: '
, num2str(I)])
Midpoint rule estimate: 34.7053
6
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Question 5 (10%)
Consider the data set of seven points shown in the following table. a) (5%) Employ the trapezoidal rule to estimate the integration.
% Data points
x = [0, 0.05, 0.15, 0.25, 0.35, 0.0475, 0.6];
y = [2, 1.856, 1.597, 1.375, 1.183, 0.991, 0.813];
% Trapezoidal rule
n = length(x);
integral_estimate = 0;
for i = 1:n-1
h = x(i+1) - x(i);
integral_estimate = integral_estimate + (y(i) + y(i+1)) * h / 2;
end
% Display the result
integral_estimate
integral_estimate = 0.7151
a) (5%) Determine the absolute error compared to Simpsons 1/3 rule integration using a cubic polynomial.
% Simpson's 1/3 rule integration using a cubic polynomial
integral_simpson = integral(@(t) interp1(x, y, t, 'spline'
), min(x), max(x));
% Calculate the absolute error
absolute_error = abs(integral_simpson - integral_estimate);
% Display the result
absolute_error
absolute_error = 0.3610
7
Question 6 (25%):
Two very important quantities in studying the growth of microorganisms in fermentation processes are the carbon dioxide evolution rate and the oxygen uptake rate. These are calculated from experimental analysis of the inlet and exit gases of the fermentor, and the flow rates, temperature, and pressure of these gases. The ratio of carbon dioxide evolution rate to oxygen uptake rate yields the respiratory quotient
, which is a good barometer of the metabolic activity of the microorganism. In addition, the above rates can be integrated to obtain the total amounts of carbon dioxide produced and oxygen consumed during the fermentation. These total amounts form the basis of the material balancing techniques used in modeling of fermentation processes. The table shows a set of rates calculated from the fermentation of Penicillium chrysogenum, which produces penicillin antibiotics.
a) (10%) Model the data as a spline polynomials and plot them along with datapoints.
% Given data
time = [140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150];
co2_rate = [15.72, 15.53, 15.19, 16.56, 16.21, 17.39, 17.36, 17.42, 17.6, 17.75, 18.95];
oxygen_rate = [15.49, 16.16, 15.35, 15.13, 14.2, 14.23, 14.29, 12.74, 14.74, 13.68, 14.51];
% Create spline interpolation
co2_spline = spline(time, co2_rate);
oxygen_spline = spline(time, oxygen_rate);
% Create a finer time grid for plotting
t_finer = linspace(140, 150, 1000);
% Evaluate the spline polynomials at finer time points
co2_spline_values = ppval(co2_spline, t_finer);
oxygen_spline_values = ppval(oxygen_spline, t_finer);
8
% Plot the data and spline polynomials
figure;
plot(time, co2_rate, 'o'
, t_finer, co2_spline_values, '-'
);
title(
'Carbon Dioxide Rate vs. Time'
);
xlabel(
'Time (h)'
);
ylabel(
'Carbon Dioxide Rate (g/h)'
);
legend(
'Data Points'
, 'Spline Polynomial'
);
figure;
plot(time, oxygen_rate, 'o'
, t_finer, oxygen_spline_values, '-'
);
title(
'Oxygen Uptake Rate vs. Time'
);
xlabel(
'Time (h)'
);
ylabel(
'Oxygen Uptake Rate (g/h)'
);
legend(
'Data Points'
, 'Spline Polynomial'
);
9
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
b) (5%) Find the rates of carbon dioxide produced and oxygen consumed at time of fermentation = 151 h.
% Interpolate at t = 151 hours
t_interpolate = 151;
co2_rate_151 = ppval(co2_spline, t_interpolate);
oxygen_rate_151 = ppval(oxygen_spline, t_interpolate);
fprintf(
'Rates at t = 151 h:\n'
);
Rates at t = 151 h:
fprintf(
'Carbon Dioxide Rate: %.2f g/h\n'
, co2_rate_151);
Carbon Dioxide Rate: 22.57 g/h
fprintf(
'Oxygen Uptake Rate: %.2f g/h\n'
, oxygen_rate_151);
Oxygen Uptake Rate: 26.25 g/h
c) (5%) Using Simpson's 1/3 rule, calculate the total amounts of carbon dioxide produced and oxygen consumed during this ten-hour period of fermentation.
% Define the time interval [a, b] for the ten-hour period
a = 141; % Start time
10
b = 151; % End time
% Number of subintervals (even for Simpson's 1/3 rule)
n = 4;
% Calculate the step size
h = (b - a) / n;
% Calculate the sum using Simpson's 1/3 rule
sum_co2 = co2_rate(2) + co2_rate(end); % First and last terms
sum_oxygen = oxygen_rate(2) + oxygen_rate(end); % First and last terms
% Calculate the sum of even-indexed terms
for i = 2:2:n-1
sum_co2 = sum_co2 + 2 * co2_rate(i);
sum_oxygen = sum_oxygen + 2 * oxygen_rate(i);
end
% Calculate the sum of odd-indexed terms
for i = 3:2:n-2
sum_co2 = sum_co2 + 4 * co2_rate(i);
sum_oxygen = sum_oxygen + 4 * oxygen_rate(i);
end
% Apply Simpson's 1/3 rule formula
integral_co2 = (h / 3) * sum_co2;
integral_oxygen = (h / 3) * sum_oxygen;
fprintf(
'Total Carbon Dioxide Produced: %.2f g\n'
, integral_co2);
Total Carbon Dioxide Produced: 54.62 g
fprintf(
'Total Oxygen Consumed: %.2f g\n'
, integral_oxygen);
Total Oxygen Consumed: 52.49 g
d) (5%) Compare the results of this function and those obtained by using adaptive quadrature
.
% Define the function handles for the spline polynomials
co2_poly = @(t) ppval(co2_spline, t);
oxygen_poly = @(t) ppval(oxygen_spline, t);
% Define the integration limits
a = 141; % Start time
b = 151; % End time
% Calculate the integrals using MATLAB's integral function
integral_co2 = integral(co2_poly, a, b);
integral_oxygen = integral(oxygen_poly, a, b);
11
fprintf(
'Total Carbon Dioxide Produced (using integral): %.2f g\n'
, integral_co2);
Total Carbon Dioxide Produced (using integral): 172.96 g
fprintf(
'Total Oxygen Consumed (using integral): %.2f g\n'
, integral_oxygen);
Total Oxygen Consumed (using integral): 148.34 g
12
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help