Finding and displaying the intersection of two functions

I want my program to find the intersection between two functions. If there are multiple solutions for this, I want it to display multiple solutions.
My code is currently like this:
function polarisatiecurve
P = 600;
I = 0:1:30;
U1 = P./I;
U2 = 20*I.^2 - 2*I.^3;
plot(I,U1,I,U2);
axis ([0 30 0 200])
xlabel('Stroom (A)')
ylabel('Spanning (V)')
title('Polarisatiecurve')
grid on
end
I need to find the intersection of U2 and U1. I already tried this:
C = intersect(U1,U2)
But that gave me the following:
C =
Empty matrix: 1-by-0
How do I fix this?

 Risposta accettata

Mischa Kim
Mischa Kim il 16 Gen 2014
Modificato: Mischa Kim il 16 Gen 2014
Use
P = 600;
fzero(@(I) 20*I.^2 - 2*I.^3 - P./I, 10)
ans =
9.6680
fzero(@(I) 20*I.^2 - 2*I.^3 - P./I, 3)
ans =
3.6069
and simply add two more data points to the plot

4 Commenti

That seems to work, thank you. But could you try to explain it a bit? I don't want to copy other people's work without knowing at least a bit what's going on.
Sure. The difference in U values at the intersection is zero. Therefore you can turn your problem into a root finding problem of U2-U1. The function that does that is called fzero, which, however, only finds one root at a time. The 10 and 3 are the starting values MATLAB uses to search for the roots. For more complex root finding problems it is often a challenge to find good starting values for the search to converge.
I see. But can it also display the Y values? Or do I have to take the inverse of the function and then make it do the same but with the inverse formula?
Forgot about the plotting part. Here you go:
P = 600;
my_fun = @(I) P./I;
my_fun_diff = @(I) 20*I.^2 - 2*I.^3 - P./I;
x1 = fzero(my_fun_diff, 3);
y1 = feval(my_fun, x1);

Accedi per commentare.

Più risposte (1)

Hasan Ballouk
Hasan Ballouk il 17 Dic 2021
Modificato: Hasan Ballouk il 28 Gen 2022
hi every body,
I have two functions here f(x1) and f(x2).
Can I add constrains in Matlab, so that I let them meet in (0,0)?
best regards
Ballouk

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by