Problem with mesh grid and intersection of two surfaces
3 views (last 30 days)
Show older comments
Hello everyone,
I’m trying to plot the intersection points between two second-order polynomial equations. These equations depend on two variables, a and b, which I define as vectors.
To evaluate the solutions, I use meshgrid to create all combinations of a and b. However, this leads to an issue: for a single root (solution), the same value of a appears repeatedly in the result. This causes my plot to show vertical lines instead of individual points, since multiple identical a values are associated with different b values (or vice versa).
Ideally, for each unique pair (a, b), I want to obtain a unique solution. But using meshgrid evaluates all possible combinations, including repeated values for a or b.
I’d appreciate any advice on how to better approach this problem, particularly in terms of how to structure the evaluation and plotting to avoid this repetition and get cleaner, more accurate plots.
Thanks in advance!
5 Comments
Catalytic
on 1 Aug 2025
Edited: Catalytic
on 1 Aug 2025
My ptoblem, is that, i'm having multiple times the same value ffor a and b for the same root.
Are you trying to develop a mapping from (a,b) to the roots, or the other way around? If you are mapping from (a,b) to the roots then it shouldn't matter if there are multiple (a,b) that map to the same root.
If you are mapping from the roots back to (a,b) then you first have to map the roots to a set of polynomial coefficients and then map the coefficients to (a,b). Bear in mind though that polynomial coefficients are unique only up to a scale factor - so you need to decide on some way to remove that ambiguity, before you can map the coefficients to a unique (a,b).
Accepted Answer
Catalytic
on 2 Aug 2025
Edited: Catalytic
on 2 Aug 2025
Effectively, I want to develop a mapping from (a, b) to the roots
If so, then @Matt J already told you what you are doing wrong. (a,b), or I guess (N11,N13), should be on the xy axis -
pas = 0.001;
[N11, N31] = meshgrid(0:pas:0.01);
%% Polynôme 1
A1 = N11.^2;
B1 = 2 .* N11 .* N31.^2;
C1 = (5 .* N11.^2 + 3 .* N31);
%% Polynôme 2
A2 = N31.^2;
B2 = N31 + N11 .* N31.^3;
C2 = N31 .* N11.^2 + N31 .* N11.^3 + 1;
%% Intersection
A = A1 - A2;
B = B1 - B2; %normalize leading coefficient
C = C1 - C2; %normalize leading coefficient
D=B.^2-4.*A.*C; %Discriminant
R1=(-B-sqrt(D))./2./A; %First root
R2=(-B+sqrt(D))./2./A; %Second root
k=D>0 & (A|B);
R1=R1(k); R2=R2(k); N11=N11(k); N31=N31(k);
scatter3(N11,N31,R1,'.b'); hold on
scatter3(N11,N31,R2,'.r'); hold off
xlabel('N11');
ylabel('N12');
zlabel('ROOT');
legend ROOT1 ROOT2
0 Comments
More Answers (1)
Matt J
on 1 Aug 2025
Edited: Matt J
on 1 Aug 2025
Perhaps as follows:
tol = 1e-8;
pas = 0.001;
[N11, N31] = meshgrid(0:pas:0.01); N11=N11(:); N31=N31(:);
%% Polynôme 1
A1 = N11.^2;
B1 = 2 .* N11 .* N31.^2;
C1 = (5 .* N11.^2 + 3 .* N31);
%% Polynôme 2
A2 = N31.^2;
B2 = N31 + N11 .* N31.^3;
C2 = N31 .* N11.^2 + N31 .* N11.^3 + 1;
%% Intersection
A = A1 - A2;
B = (B1 - B2)./A; %normalize leading coefficient
C = (C1 - C2)./A; %normalize leading coefficient
D=B.^2-4*C; %Discriminant
R1=(-B-sqrt(D))/2; %First root
R2=(-B+sqrt(D))/2; %Second root
%Post-process: discard redundant or illegal solutions
T=[R1,R2,N11,N31];
T( any(~isfinite(T),2) | D<0 , : )=[]; %discard non-finite or complex solutions
[~,Iu]=uniquetol(T(:,1:2),tol,'ByRows',true); %discard repeat solutions (within tolerance)
T=num2cell(T(Iu,:),1);
[R1,R2,N11,N13]=deal(T{:});
figure
scatter3(R1,R2,N11,'.b');
xlabel('ROOT #1');
ylabel('ROOT #2');
zlabel('N11');
figure
scatter3(R1,R2,N13,'.b');
xlabel('ROOT #1');
ylabel('ROOT #2');
zlabel('N13');
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



