10.1.1: Using fzero() to find the intersection of 2 functions
- Page ID
- 88611
Using fzero() to find the intersection of 2 functions
Assume you have functions y1(x) and y2(x). Create a new function y3(x) = y1(x) - y2(x). The zero of y3(x) is the intersection of y1(x) and y2(x).
We will have 2 files. A test script file, and a function file.
% A. In the test script m-file, open a new figure and plot these 2 functions on it:
x = (0:0.05:1)*pi;
y1 = 0.3*x;
y2 = cos(2*x);
figure(1)
plot(x,y1)
hold on;
plot(x,y2)
grid on;
% We can see that one intersection of these functions is near x = 0.7 and y = 0.2
%% Create a function file, f_3x_cos2x.m, that computes y = y1(x) - y2(x)
function y = f_3x_cos2x(x)
y1 = 0.3*x;
y2 = cos(2*x);
y = y1 - y2;
end
% The zero(s) of this single function is the intersection point(s) of the 2 functions.
% In the test script file, call fzero_3x_cos2x(x) and plot the result.
yy3 = fzero_3x_cos2x(x);
figure(2)
plot(x,yy3)
grid on;
title('y3 = y1 - y2')
% We can see that one zero is near x = 0.7 and y3 = 0.0
%% Use Matlab's fzero function to find the roots of y3
x_solution = fzero(@f_3x_cos2x, 0.7) % near 0.7
% 0.6823
% Verify that this is a solution
y1b = 0.3*x_solution % 1st function
% 0.2047
y2b = cos(2*x_solution) % 2nd function
% 0.2047
%% Plot the solution point on the 1st figure
figure(1)
hold on;
plot(x_solution,y1b,'o')
plot(x_solution,y2b,'*')
Add exercises text here.
- Answer
-
Add texts here. Do not delete this text first.
Solution
Add example text here.
.
Intersection of quadratic and cubic functions
A. (2 pts) Create these 2 function files:
y_fun_x2(x)
y_x2 = x.^2 -4;
end
function y_x3 = y_fun_x3(x)
y_x3 = x.^3 - 6*x;
end
B1. (2 pt) Begin a script m-file named fzero_y_fun_x2_x3_YourName.m
that will plot these functions and will solve for one of their intersections.
At the top of this script, include this code:
clear all; clear functions; close all; format compact; clc;
B2a. (1 pt) Create an x vector from -3 to 3
Choose an increment for x = 0.3, so that the plot will be smooth.
B2b. (2 pts) Use this x vector to compute the corresponding values of y for each of the 2 functions.
y2 = y_fun_x2(x);
y3 = y_fun_x3(x);
B3. (2 pts) Open a figure and plot y2, add "hold on;", the plot y3 on the figure. You will see that they intersect at 3 places.
C1. (2 pts) fzero() finds a root of a single function, so create a 3rd function that computes the difference of the 1st 2 functions:
function y23 = y_2_3_fun(x)
y23 = y_fun_x2(x) - y_fun_x3(x);
end
Use the x vector to compute values of y for the difference function:
y23 = y_2_3_fun(x)
C2. (1 pt) Open a 2nd figure and plot(x, y23)
D. (6 pts) Use the fzero() function to find a solution where y_2_3_fun(x) = 0.
Use the function handle, @y_2_3_fun
as the 1st input of fzero().
For the initial value of x, choose an integer value (whole number) near any one of the intersection points. (Do not use a decimal value that is not an integer.)
- Answer
-
Add texts here. Do not delete this text first.
.
fzero_exp
A. Set
x = (0 : 0.05 : 1)
B. Compute these 2 functions:
y1 = 0.7x
y2 = exp(-2*x)
C. Open a new figure and plot these 2 functions on it.
plot(x,y1)
hold on;
plot(x,y2)
The intersection of these 2 functions is the solution of this transcendental equation:
y1 = y2 or, equivalently, y1 - y2 = 0
D. Find the value of x near 0.5 that is a solution of this equation using the fzero() function as follows:
D1. Rewrite it in the form: fexp(x) = exp(-2*x) - 0.7*x, then create a function file called fexp.m that computes this.
D2. Solve it using the fzero() function, using @fexp as the function handle.
- Answer
-
Add texts here. Do not delete this text first.
.