1) I need a code to determine the intersection points, I have tried '==' is not working, also I would like to know if it is possible to make the grid line look like a graph sheet i.e using Xtick and Ytick

4 vues (au cours des 30 derniers jours)
  4 commentaires
Ezekiel  Omoniyi
Ezekiel Omoniyi le 14 Juin 2019
Thanks alot, the graph is a bit easier to read now but would like to know the code to set ticks to exact point of intercepts manually.trial test graph .jpg
dpb
dpb le 14 Juin 2019
Use interp1 to find intersection points and then just add those to the existing XTick value vector. You will have to sort that to write them out.
But, that's probably not what you really want is having another tick mark because the intersections appear to almost identically on the existing ticks at 0.38 and 0.46 -- unless you're talking y-axis, maybe??? But even there, it'll get pretty crowded on the axis to insert another tick there.
I'd say you'd be better off to just put a marker and text annotation at or near the points instead...

Connectez-vous pour commenter.

Réponse acceptée

James Browne
James Browne le 15 Juin 2019
Modifié(e) : James Browne le 15 Juin 2019
Greetings,
I believe I may know what is causing your problem and I might know how to solve it.
Consider the following: Although, when you plot two intersecting functions on a graph, you can clearly see that, at some point, they are equal. However, you also need to keep in mind that the plot is just a connect-the-dots way of representig data. if the intersection point is the result of the two functions having EXACTLY the same value in the data sets, then yes, you can use "==" to find where they intersect.
If, however the intersection on the plot is simply where lines (which are connecting data points) intersect, then "==" will not get you any results at all!
For example, try running the following code:
x = [0 1 2 3 4];
y1 = 1.5*x -1
y2 = 0.3*x
plot(x,y1,x,y2)
Now, when you look at the plot, you can clearly see that there is some point where the lines cross, therefore, there is some point where the finctions y1 and y2 are equal. However, if you look at the vectors for y1 and y2, you can clearly see that there is no value in y1 which is the same as any value in y2. This is simply because the x-vector does not contain the value which causes a identical values to appear in the y1 and y2 vectors.
The most accurate way to find the point of intersection between y1 and y2 is to set y1(x) equal to y2(x) and solve for x, though I am not sure how to do this with MATLAB, most hand held calculators can accomplish this.
The less accurate (and most fun) way to find this point of intersection between y1 and y2 is to write a MATLAB scrip which will iterate through values of x, for the range of x-values that you think the correct answer is within, and pick out the x-value which produces the smallest error between y1(x) and y2(x).
For example, if I were to use the iteration method to solve for the intersection point of y1(x) and y2(x) from the example above, I would do the following:
1) Plot y1(x) and y2(x) on the same figure.
2) From the plot, get a range of x- values which will contain the intersection point for y1 and y2 ( 0.5 < x < 1.5, in this case).
3) Write a script that iterates through x-values over the range of x that was determined in step 2, compares resulting y1 and y2 values and saves the x-value which produces the smallest error between y1(x) and y2(x).
4) Turn in homework.
Example iteration script:
x = [0 1 2 3 4];
y1 = 1.5*x -1;
y2 = 0.3*x;
xRange1 = 0.5;
xRange2 = 1.5;
previousError = Inf;
for i = 0.5:0.0001:1.5
y1x = 1.5*i -1;
y2x = 0.3*i;
currentError = abs(y1x - y2x);
if ( currentError < previousError )
previousError = currentError;
bestX = i;
end
end
%The approximate intersect can be determined using either y1(x) or y2(x),
%in this example, y2(x) is used to determine the approximate intercept
approximateIntercept = 0.3*bestX;
fprintf('The approximate intercept occurs at x = %4.3f\n',approximateIntercept)
plot(x,y1,x,y2,bestX,approximateIntercept,'kd')
xlabel('x values')
ylabel('y values')
title('intersection of y1(x) and y2(x)')
legend('y1(x)','y2(x)','Approximate Intersection of y1(x) and y2(x)','location','southeast')
  2 commentaires
Stephen23
Stephen23 le 15 Juin 2019
"The most accurate way to find the point of intersection between y1 and y2 is to set y1(x) equal to y2(x) and solve for x, though I am not sure how to do this with MATLAB"
fzero
James Browne
James Browne le 15 Juin 2019
Oh yea! I totally forgot about fzero ahhahahahahaha! Thank you! It is not quite as fun as wasting time in for-loop-land though, IMO~

Connectez-vous pour commenter.

Plus de réponses (2)

Stephen23
Stephen23 le 15 Juin 2019
Modifié(e) : Stephen23 le 15 Juin 2019
Use fzero to find the intersection of two functions.
Method one: functions
>> F1 = @(x)1.5*x-1;
>> F2 = @(x)0.3*x;
>> X = 0:4;
>> plot(X,F1(X),X,F2(X))
Now find the intersection:
>> yi = fzero(@(x)F1(x)-F2(x),1)
yi = 0.83333
And checking:
>> F1(yi)
ans = 0.25000
>> F2(yi)
ans = 0.25000
Method two: interpolate numeric data
If you have data values rather than functions then you can interpolate the data and use fzero:
>> Y1 = F1(X)
Y1 =
-1.0000 0.5000 2.0000 3.5000 5.0000
>> Y2 = F2(X)
Y1 =
-1.0000 0.5000 2.0000 3.5000 5.0000
>> G1 = @(x)interp1(X,Y1,x);
>> G2 = @(x)interp1(X,Y2,x);
>> yi = fzero(@(x)G1(x)-G2(x),1)
yi = 0.83333
And checking:
G1(yi)
ans = 0.25000
G2(yi)
ans = 0.25000

Ezekiel  Omoniyi
Ezekiel Omoniyi le 16 Juin 2019
Good evening,
Thanks for all the responses from all who have spare their time to contribute so far, I want to say I am indeed grateful and I have broaden my knowledge base through your various answers. However, I would like know if it is possible to make the graph look like the one I have printed below. I more interested in the code in matlab 2018a. Please kindly be a little explicit in your explanation.
Thanks in anticipation of your favourable responses.example of matlab.jpg

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by