algorithm


intersection MATLAB, multiple roots


I am having problems determining this basic logic. Given 2 functions: y1 and y2, plotted on x in MATLAB. How do you determine the intersections using simple for loop and if else statement. These y1 and y2 has more than one intersections. I am pretty sure that I am missing something in the loop
clc
clear
x = linspace(0,2);
y1 = 2.*x + 1;
y2 = exp(x);
tol = 0.05;
x_intercept = zeros(size(x));
y_intersect = zeros(size(x));
for i = 1:100
if abs(y1(i) - y2(i)) == tol
y_intersect = y2(x(i));
x_intercept = x(i);
end
end
plot(x,y1)
hold on
plot(x,y2)
plot(x_intercept, y_intersect,'xr');
Could you please offer help? I am sorry if this seems to be a very easy question but I have searched and found no answer. All I found is using polyval/polyfit and the likes but those only show 1 intersection.
Try changing your for loop to:
ctr=1;
for i = 1:100
if abs(y1(i) - y2(i)) <= tol
y_intersect(ctr) = y2(i);
x_intercept(ctr) = x(i);
ctr=ctr+1;
end
end
You can use the function solve to find the points of intersection of the two curves:
clc
clear
% Define the symbolic variables
syms x y
vars=[x y]
% Define the two eqautions
equations=([2*x+1-y == 0,exp(x)-y == 0])
% Call SOLVE to find the intersection point
[sol_x,sol_y]=solve(equations,vars,'Real', true)
% Get the values of the x and y coordinates of the intersectiin points
x_inters=double(sol_x)
y_inters=double(sol_y)
% Evaluate the two functions (only to plot them)
x = linspace(0,2);
y1 = 2.*x + 1;
y2 = exp(x);
plot(x,y1)
hold on
plot(x,y2)
% Add the intersection points
plot(x_inters,y_inters,'or','markerfacecolor','r')
If you want / need to use for and if-else statements, you need to, first, modify the if condition in you code:
if abs(y1(i) - y2(i)) <= tol
then you have to increment the number of samples x in order to reduce the distance between them.
Also you have to test different values for the threshold tol.
This approach will identify several solutions, so you shouls also identify, among then, the ones for which the difference between the values of y1 and y2 is the lower.
A possible implementation could be:
clc
clear
% x = linspace(0,2);
% Define the x samaples
x=0:.001:2
y1 = 2.*x + 1;
y2 = exp(x);
% tol = 0.05;
tol = 0.001;
x_intercept = zeros(size(x));
% y_intersect = zeros(size(x));
y1_intersect = zeros(size(x));
y2_intersect = zeros(size(x));
% Initialize the counters
cnt=0;
once=0;
% Initialize the minimun_difference
min_diff=999;
% for i = 1:100
% Loop over the xsamples
for i = 1:length(x)
% if abs(y1(i) - y2(i)) == tol
y1_y2_diff=abs(y1(i) - y2(i));
if(y1_y2_diff <= tol)
% If the difference is lower than the threshold, set the flag to
% increment the number of solutions
if(~once)
cnt=cnt+1;
once=1;
end
% Store the values for the minimum difference
if(y1_y2_diff <= min_diff)
min_diff=y1_y2_diff;
y1_intersect(cnt) = y1(i);
y2_intersect(cnt) = y2(i);
x_intercept(cnt) = x(i);
end
else
% Rese the flag
min_diff=999;
once=0;
end
end
plot(x,y1)
hold on
plot(x,y2)
% plot(x_intercept, y_intersect,'xr');
plot(x_intercept(1:cnt), y1_intersect(1:cnt),'xr');
plot(x_intercept(1:cnt), y2_intersect(1:cnt),'dr');
Hope this helps,
Qapla'

Related Links

Compression of neighborhood graph
Post Correspondence Prob - Find correspondence while the indexes and number of words can differ
If Y is reducible to X in polynomial time, then how is it true that X is at least as hard as Y?
How to understand recursive Boundary conditions´╝č
Find the shortest fence that encloses an area on a 2D grid
1D Memoization in Recursive solution of Longest Increasing Subsequence
Lua: Markov-chain algorithm error
Calculate tree node index
Greatest Common Divisor - Upper bound
All-pair shortest path for minimum spanning tree
interpret this algorithm of discrete mathematics and its applications, Kenneth H. Rosen
Path finding with theta* when the triangle inequality is not fulfilled
Mergesort stack (using only extra stacks, but with as many as needed)
About tarjan algorithm, why can't we take low[v] instead of disc[v] when considering the backward edge?
Max flow in unweighted graph
Time-complexity of while loop in big-O

Categories

HOME
service-worker
asp.net-web-api
hyperlink
delphi-7
nvd3.js
dns
twig
rsa
themes
requirements
devise
knitr
vscode-extensions
vxml
pyqt4
ado.net
maxscript
settings
alamofire
line
angular-dart
asp.net-mvc-viewmodel
boost-icl
php-mysqlidb
mongoid6
strongname
froala
xcode-ui-testing
moses
bulletphysics
angularjs-components
underscore.js
spotipy
errbot
gp
testcomplete
calculation
scrollview
autodesk-designautomation
panoramas
mobx
anki
left-join
testbed
android-gridview
installshield-le
hunspell
installscript
html-encode
sql-delete
lubridate
servicestack-text
tex
css-position
p4merge
unordered-map
signals-slots
tasm
trigonometry
ssrs-2014
android-checkbox
facebook-social-plugins
flask-restful
context-switch
gameplay-kit
shinobi
change-tracking
node-request
cubism.js
redistributable
segment-io
grass
realsense
toran-proxy
dynatree
dock
atoi
git-ftp
snackbar
document-repository
appserver
with-statement
ora-00904
buffering
paste
bin
inputmismatchexception
nevron
kaleidoscope
css-reset
superscrollorama
booksleeve
intersect
faye
mapping-by-code
w3c-geolocation
timthumb
inline-formset
unattended-processing
appledoc
presentation-layer
maven-1
interprocess
filesystemobject
lalr
unmanagedresources

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile