15 views (last 30 days)
Show older comments
Luis Canales Tough on 20 Apr 2016
-
-
Link
Direct link to this question
https://matlabcentral.mathworks.com/matlabcentral/answers/280113-intersection-of-parabola
Commented: Meade on 21 Apr 2016
Hello,
I am trying to find the intersection of a parabolic function, not graphically, but numerically. I have a parabolic function f(x), I want to find BOTH points of intersection for a value of 'y'. The way I have tried to do it, only returns one value, it is as follows:
>f=f(x);
>y = 0.6; %value to find
>inter = abs(f-y)
>[idx idx] = min(inter); %index of closest value
>closest = f(idx) %closest value %%%%%%%%%%%%%%%%%%%
that only returns one of the closest values, I know for a fact that at that point on 'y' it should intersect at two points on the parabola.
I am not sure how to approach this.
Thanks in advance.
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Answers (2)
Star Strider on 20 Apr 2016
⋮
Open in MATLAB Online
You know the equation for the parabola and the value you want to solve for, so just use that information and the roots function:
quad_coefs = [2 -4 -3]; % Parabola (Quadratic) Coefficients
const = 10; % Desired Value
subt_const = quad_coefs - [0 0 const]; % Subtract Desired Value
orig_roots = roots(quad_coefs) % Original Roots
new_roots = roots(subt_const) % Values (Roots) At ‘y=10’
x = linspace(-5, 5);
figure(1)
plot(x, polyval(quad_coefs,x))
hold on
plot(xlim, [1 1]*const)
hold off
grid
text(new_roots(1), const, sprintf('x = %.3f \\downarrow',new_roots(1)), 'HorizontalAlignment','right', 'VerticalAlignment','bottom')
text(new_roots(2), const, sprintf('\\downarrow x = %.3f',new_roots(2)), 'HorizontalAlignment','left', 'VerticalAlignment','bottom')
You mentioned that you don’t need the plot for your application. However, it helps to use one to show the results:
This code also works for complex roots, but the plot will be irrelevant with them.
2 Comments Show NoneHide None
Show NoneHide None
Luis Canales Tough on 20 Apr 2016
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/280113-intersection-of-parabola#comment_360405
Well, the equation defining the parabola is of high complexity and with various variables, so finding the coefficients proves to be quite difficult. However in the matrix, there should be two values that correspond to the constant which I am inputting, so how may I find that? Maybe with the 'find' function? However, the help on mathworks is not very clear on how to approach this particular problem.
I tried your way, but because the function being subtracted is of size 1x1000 then I dont know how to substract a matrix that large.
Many thanks for your help.
Star Strider on 21 Apr 2016
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/280113-intersection-of-parabola#comment_360443
⋮
Edited: Star Strider on 21 Apr 2016
I need to understand something. You said ‘parabola’, so that implies a second-degree polynomial, at least to me. It may be a function of several variables, but it should still be a second-degree polynomial, or data modeled by a second-degree polynomial. It should not be all that difficult to use polyfit to estimate its parameters.
If your curve is something else, knowing what it is would be helpful.
EDIT — It would have been quite helpful to know that your ‘parabola’ really isn’t a parabola, as you mentioned in your duplicate Question: http://www.mathworks.com/matlabcentral/answers/280131-finding-the-intersection-of-a-line-and-a-parabola-numerically.
Not cool.
Sign in to comment.
Meade on 20 Apr 2016
Open in MATLAB Online
Instead of min, try sort to get the values and indices of your logical condition. For ex.
[sVals, sIdx] = sort(inter); % All values in order from small to large
twoMins = sVals(1:2); % Only the 2 smallest
closest1 = f(sIdx(1)); % the value of "f" at the two smallest
closest2 = f(sIdx(2));
2 Comments Show NoneHide None
Show NoneHide None
Luis Canales Tough on 20 Apr 2016
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/280113-intersection-of-parabola#comment_360406
I am not really sure how to apply this to the problem I am encountering.
Nevertheless, many thanks for your time and your help.
Meade on 21 Apr 2016
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/280113-intersection-of-parabola#comment_360590
You have common x values for the two curves. The differences are the y values for the two curves.
You've already found the absolute difference in those y values and saved it to the "inter" variable. You're so close!
Now you use sort to put all those differences in order from smallest to largest. You said that you know there are 2 intersections of interest, so you want the first 2 results from sort. Make sense?
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABLanguage FundamentalsMatrices and ArraysCreating and Concatenating Matrices
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Tags
- parabola
- intersection
- two points
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office