Assignment6_MariaCGonzalez1
.pdf
keyboard_arrow_up
School
Florida International University *
*We aren’t endorsed by this school
Course
4051L
Subject
Mathematics
Date
Apr 3, 2024
Type
Pages
8
Uploaded by ChefNightingale3065 on coursehero.com
Name:Maria C Gonzalez Date: 11/15/2023
PID: 6344822
Assignment 6 : Nonlinear Equations and Optimization
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_7_AsadMirza. To convert the MLX file into a PDF go to the Export icon under Live Editor and select "Export to PDF".
Question 1 (15%): Consider the equation:
a) (5%) Graph the equation and identify the number of zeros.
b) (15%) Use Newton’s method to find the zeros of f(x) from -2 to 4 and the x-values at which this occurs.
Hint: Choose a proper x0 based on the graph to find one and run again to find the other.
% Define the function
f = @(x) exp(-x.^2) - (x-1).^4 + 10;
% Plot the function over a range of x values
x = linspace(-2, 4, 1000);
y = f(x);
plot(x, y)
grid on
1
% Find the zeros of the function
zero1 = fzero(f, -1);
zero2 = fzero(f, 2);
fprintf(
'The equation has %d zeros.\n'
, length([zero1 zero2]));
The equation has 2 zeros.
% Define the function and its derivative
myFunction = @(x) exp(-x.^2) - (x-1).^4 + 10;
myDerivative = @(x) -2*x.*exp(-x.^2) - 4*(x-1).^3;
% Define the Newton's method function
myNewton = @(f, df, x0, tol) myNewtonHelper(f, df, x0, tol, 0);
% Iterate through Newton's method for each x0 value
x0Values = linspace(-2, 4, 100);
zeros = [];
for i = 1:length(x0Values)
x0 = x0Values(i);
[zero, iter] = myNewton(myFunction, myDerivative, x0, 1e-6);
% Check if the zero is unique and within the desired range
if ~isnan(zero) && zero > -2 && zero < 4 && ~ismember(zero, zeros)
zeros(end+1) = zero;
2
fprintf(
'Zero found at x = %.6f after %d iterations.\n'
, zero, iter);
end
end
Zero found at x = -0.801227 after 5 iterations.
Zero found at x = -0.801227 after 5 iterations.
Zero found at x = -0.801227 after 5 iterations.
Zero found at x = -0.801227 after 5 iterations.
Zero found at x = -0.801227 after 4 iterations.
Zero found at x = -0.801227 after 4 iterations.
Zero found at x = -0.801227 after 4 iterations.
Zero found at x = -0.801227 after 4 iterations.
Zero found at x = -0.801227 after 3 iterations.
Zero found at x = -0.801227 after 3 iterations.
Zero found at x = -0.801227 after 3 iterations.
Zero found at x = -0.801227 after 3 iterations.
Zero found at x = -0.801227 after 4 iterations.
Zero found at x = -0.801227 after 5 iterations.
Zero found at x = -0.801227 after 6 iterations.
Zero found at x = -0.801227 after 7 iterations.
Zero found at x = 2.778299 after 16 iterations.
Zero found at x = 2.778299 after 11 iterations.
Zero found at x = 2.778299 after 11 iterations.
Zero found at x = 2.778299 after 11 iterations.
Zero found at x = 2.778299 after 12 iterations.
Zero found at x = 2.778299 after 12 iterations.
Zero found at x = 2.778299 after 12 iterations.
Zero found at x = 2.778299 after 12 iterations.
Zero found at x = 2.778299 after 11 iterations.
Zero found at x = 2.778299 after 9 iterations.
Zero found at x = 2.778299 after 6 iterations.
Zero found at x = 2.778299 after 5 iterations.
Zero found at x = 2.778299 after 4 iterations.
Zero found at x = 2.778299 after 4 iterations.
Zero found at x = 2.778299 after 3 iterations.
Zero found at x = 2.778299 after 3 iterations.
Zero found at x = 2.778299 after 4 iterations.
Zero found at x = 2.778299 after 4 iterations.
Zero found at x = 2.778299 after 5 iterations.
Zero found at x = 2.778299 after 5 iterations.
% Define the helper function for Newton's method
function [zero, iter] = myNewtonHelper(f, df, x, tol, iter)
% Calculate the next guess using Newton's method
xNext = x - f(x) / df(x);
% Check if the tolerance has been reached
if abs(xNext - x) < tol
zero = xNext;
return
end
% Recursively call the function with the new guess
[zero, iter] = myNewtonHelper(f, df, xNext, tol, iter+1);
end
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