How do I create surface between curves of differing amounts of data points

3 Ansichten (letzte 30 Tage)
Rowan
Rowan am 2 Jul. 2024
Kommentiert: Rowan am 2 Jul. 2024
I am trying to plot curves in seperate planes and draw surfaces connecting them. so far i have managed this with;
x1=linspace(0, pi, 100)
x2=linspace(0, pi, 100)
z1=sin(x1)
z2=sin(x2)
k1=ones(1, length(x1))
k2=2*ones(1, length(x2))
plot3(k1, x1, z1); hold on; plot3(k2, x2, k2)
surf([k1;k2], [x1;x2], [z1;z2], 'meshstyle', 'row')
However, the surf function isnt working when I create x2 and x1 with differing numbers of data points such as;
x1=linspace(0, pi, 50)
x2=linspace(0, pi, 25)
Thurthermore any way to smooth the boundaries of the curves would be greatly appreciated as im am very new to this. Thanks in advance.

Antworten (1)

Tony
Tony am 2 Jul. 2024
Bearbeitet: Tony am 2 Jul. 2024
One method is to expand to use the union of reference data points, and then apply the two functions to the union reference
x1=linspace(0, pi, 50);
x2=linspace(0, pi, 25);
x12 = unique([x1 x2]);
z1=sin(x12);
z2=sin(x12);
k1=ones(1, length(x12));
k2=2*ones(1, length(x12));
figure(1); tiledlayout(1, 2);
nexttile; plot3(k1, x12, z1); hold on; plot3(k2, x12, z2);
nexttile; surf([k1;k2], [x12;x12], [z1;z2], 'meshstyle', 'row');
But if you don't have the original function, you can interpolate the data
x1=linspace(0, pi, 50);
x2=linspace(0, pi, 25);
z1=sin(x1);
z2=sin(x2);
x12 = unique([x1 x2]);
z1_12 = interp1(x1, z1, x12);
z2_12 = interp1(x2, z2, x12);
k1=ones(1, length(x12));
k2=2*ones(1, length(x12));
figure(2); tiledlayout(1, 2);
nexttile; plot3(k1, x12, z1_12); hold on; plot3(k2, x12, z2_12);
nexttile; surf([k1;k2], [x12;x12], [z1_12;z2_12], 'meshstyle', 'row');
  1 Kommentar
Rowan
Rowan am 2 Jul. 2024
thank you, this is extremely helpful. i have used the second method as i intend to use this on real data and this is practice/testing what is possible and have had success with creating a surface like above.
However, i have ran into an issue and found that i need the domain, x1 and x2, to start and end at the same point regardless of if they have the same number of points or not. For example, say you have two functions z1 and z2 where z1 has 100 data entries and z2 has 50. i have sets x1 and x2 which are just a series of integers created by;
x1=reshape(1:100, 1, 100)
x2=reshape(1:50, 1, 50)
I belive this creates a problem when using
z2_12 = interp1(x2, z2, x12)
as x12 now has entreis greater than the greatest in x2. x12 is just x1 as it x2 is a subset of points of x1 and therefore the 51st entry and any subsequent one is just "NaN". I'd assume this is because x2 is 1 through 50 but x12 is 1 thorugh 100. I have circumvented this by creating x2 by
x2=2*reshape(1:50, 1, 50)
so that x2 is 2, 4, 6, ..., 100. However this isnt wouldnt be suitlible for real data. If you have any fix to this that would be amazing i clearly have no clue what im doing. Thanks again.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by