From making 2D line plots to making 3D surface plots: How to preserve my current workflow?

4 ビュー (過去 30 日間)
Hi there!
Below, I think the explanation, originally a comment by me, is more clear, so I am posting it here as the question.
Just to describe my workflow a bit more clearly: I loop through beta and, for each beta, I calculate values of F, a scalar-valued function, for all alpha = linspace(0, 2pi, 1001). I am only looping through beta, not alpha, which I really like. (I notice that nested loops can cause the code to take much longer to run.) I might choose 10 or 11 evenly spaced values of beta to loop through, using linspace( ).
Then, I made nice line plots, going from subplots to tiled layouts, which seems to be advantageous; e.g. global title, global colorbar, etc. are made easy using tiledLayout rather than with subplot. In my 2D line plots, each tile would have the 11 curves, with alpha being the independent variable, and I use colors from a colormap to show the variation in beta. This figure of 6 tiles, I think, is really pretty -- and communicative.
Now, instead of 2D line plots, I want to present this exact same information using 3D surface plots, just to compare and see which figure is more communicative. However, since my functions that I am plotting are scalar-valued functions, I am getting the error that my functions need to be matrices. By the way, I have plenty of vectors expressed as linear combinations of basis vectors, but the final functions to be plotted are all scalar-valued, gotten from using dot products.
So, as of now, I can basically write a poor man's version of a code that gives the surface plots (very pretty), but this is not good and systematic:
  1. I would like to continue looping through beta;
  2. To keep things organized and de-cluttered, I would like to continue calling my functions which are stored in separate files;
  3. For doing careful mathematics, I would like to continue expressing vectors as linear combinations of basis vectors; and
  4. I would like to keep all the functions to be plotted scalar-valued.
It seems if in the beginning of my surface-plotting code I define a meshgrid [Alpha,Beta] = meshgrid(alpha,beta), and use the variables Alpha and Beta as inputs, then I start running into issues with arrays having incompatible sizes. I have one current guess at a solution, as of now: Maybe define my functions locally in the same file as my surface-plotting code, rather than call them from another separate file? I would rather not do this, because my intuition is that my errors should be easy enough to fix.
I look forward to hearing from you.
Thanks for your time!

採用された回答

Star Strider
Star Strider 2025 年 1 月 2 日
Theree is not enough information proviided to determine what the problem is. If ‘i’ and ‘j’ are scalars, or matrices conformable under matrix multiplication with ‘Alpha’ and ‘Beta’ (that have the same sizes), your code should work.
The problem is not in the quoted code.
alpha = linspace(0,pi,20)
alpha = 1×20
0 0.1653 0.3307 0.4960 0.6614 0.8267 0.9921 1.1574 1.3228 1.4881 1.6535 1.8188 1.9842 2.1495 2.3149 2.4802 2.6456 2.8109 2.9762 3.1416
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
beta = linspace(0,10,100)
beta = 1×100
0 0.1010 0.2020 0.3030 0.4040 0.5051 0.6061 0.7071 0.8081 0.9091 1.0101 1.1111 1.2121 1.3131 1.4141 1.5152 1.6162 1.7172 1.8182 1.9192 2.0202 2.1212 2.2222 2.3232 2.4242 2.5253 2.6263 2.7273 2.8283 2.9293
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[Alpha, Beta] = meshgrid(alpha, beta),
Alpha = 100×20
0 0.1653 0.3307 0.4960 0.6614 0.8267 0.9921 1.1574 1.3228 1.4881 1.6535 1.8188 1.9842 2.1495 2.3149 2.4802 2.6456 2.8109 2.9762 3.1416 0 0.1653 0.3307 0.4960 0.6614 0.8267 0.9921 1.1574 1.3228 1.4881 1.6535 1.8188 1.9842 2.1495 2.3149 2.4802 2.6456 2.8109 2.9762 3.1416 0 0.1653 0.3307 0.4960 0.6614 0.8267 0.9921 1.1574 1.3228 1.4881 1.6535 1.8188 1.9842 2.1495 2.3149 2.4802 2.6456 2.8109 2.9762 3.1416 0 0.1653 0.3307 0.4960 0.6614 0.8267 0.9921 1.1574 1.3228 1.4881 1.6535 1.8188 1.9842 2.1495 2.3149 2.4802 2.6456 2.8109 2.9762 3.1416 0 0.1653 0.3307 0.4960 0.6614 0.8267 0.9921 1.1574 1.3228 1.4881 1.6535 1.8188 1.9842 2.1495 2.3149 2.4802 2.6456 2.8109 2.9762 3.1416 0 0.1653 0.3307 0.4960 0.6614 0.8267 0.9921 1.1574 1.3228 1.4881 1.6535 1.8188 1.9842 2.1495 2.3149 2.4802 2.6456 2.8109 2.9762 3.1416 0 0.1653 0.3307 0.4960 0.6614 0.8267 0.9921 1.1574 1.3228 1.4881 1.6535 1.8188 1.9842 2.1495 2.3149 2.4802 2.6456 2.8109 2.9762 3.1416 0 0.1653 0.3307 0.4960 0.6614 0.8267 0.9921 1.1574 1.3228 1.4881 1.6535 1.8188 1.9842 2.1495 2.3149 2.4802 2.6456 2.8109 2.9762 3.1416 0 0.1653 0.3307 0.4960 0.6614 0.8267 0.9921 1.1574 1.3228 1.4881 1.6535 1.8188 1.9842 2.1495 2.3149 2.4802 2.6456 2.8109 2.9762 3.1416 0 0.1653 0.3307 0.4960 0.6614 0.8267 0.9921 1.1574 1.3228 1.4881 1.6535 1.8188 1.9842 2.1495 2.3149 2.4802 2.6456 2.8109 2.9762 3.1416
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Beta = 100×20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1010 0.1010 0.1010 0.1010 0.1010 0.1010 0.1010 0.1010 0.1010 0.1010 0.1010 0.1010 0.1010 0.1010 0.1010 0.1010 0.1010 0.1010 0.1010 0.1010 0.2020 0.2020 0.2020 0.2020 0.2020 0.2020 0.2020 0.2020 0.2020 0.2020 0.2020 0.2020 0.2020 0.2020 0.2020 0.2020 0.2020 0.2020 0.2020 0.2020 0.3030 0.3030 0.3030 0.3030 0.3030 0.3030 0.3030 0.3030 0.3030 0.3030 0.3030 0.3030 0.3030 0.3030 0.3030 0.3030 0.3030 0.3030 0.3030 0.3030 0.4040 0.4040 0.4040 0.4040 0.4040 0.4040 0.4040 0.4040 0.4040 0.4040 0.4040 0.4040 0.4040 0.4040 0.4040 0.4040 0.4040 0.4040 0.4040 0.4040 0.5051 0.5051 0.5051 0.5051 0.5051 0.5051 0.5051 0.5051 0.5051 0.5051 0.5051 0.5051 0.5051 0.5051 0.5051 0.5051 0.5051 0.5051 0.5051 0.5051 0.6061 0.6061 0.6061 0.6061 0.6061 0.6061 0.6061 0.6061 0.6061 0.6061 0.6061 0.6061 0.6061 0.6061 0.6061 0.6061 0.6061 0.6061 0.6061 0.6061 0.7071 0.7071 0.7071 0.7071 0.7071 0.7071 0.7071 0.7071 0.7071 0.7071 0.7071 0.7071 0.7071 0.7071 0.7071 0.7071 0.7071 0.7071 0.7071 0.7071 0.8081 0.8081 0.8081 0.8081 0.8081 0.8081 0.8081 0.8081 0.8081 0.8081 0.8081 0.8081 0.8081 0.8081 0.8081 0.8081 0.8081 0.8081 0.8081 0.8081 0.9091 0.9091 0.9091 0.9091 0.9091 0.9091 0.9091 0.9091 0.9091 0.9091 0.9091 0.9091 0.9091 0.9091 0.9091 0.9091 0.9091 0.9091 0.9091 0.9091
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
i = rand
i = 0.0240
j = rand
j = 0.1758
vx = cos(Alpha) * i
vx = 100×20
0.0240 0.0237 0.0227 0.0211 0.0189 0.0163 0.0131 0.0096 0.0059 0.0020 -0.0020 -0.0059 -0.0096 -0.0131 -0.0163 -0.0189 -0.0211 -0.0227 -0.0237 -0.0240 0.0240 0.0237 0.0227 0.0211 0.0189 0.0163 0.0131 0.0096 0.0059 0.0020 -0.0020 -0.0059 -0.0096 -0.0131 -0.0163 -0.0189 -0.0211 -0.0227 -0.0237 -0.0240 0.0240 0.0237 0.0227 0.0211 0.0189 0.0163 0.0131 0.0096 0.0059 0.0020 -0.0020 -0.0059 -0.0096 -0.0131 -0.0163 -0.0189 -0.0211 -0.0227 -0.0237 -0.0240 0.0240 0.0237 0.0227 0.0211 0.0189 0.0163 0.0131 0.0096 0.0059 0.0020 -0.0020 -0.0059 -0.0096 -0.0131 -0.0163 -0.0189 -0.0211 -0.0227 -0.0237 -0.0240 0.0240 0.0237 0.0227 0.0211 0.0189 0.0163 0.0131 0.0096 0.0059 0.0020 -0.0020 -0.0059 -0.0096 -0.0131 -0.0163 -0.0189 -0.0211 -0.0227 -0.0237 -0.0240 0.0240 0.0237 0.0227 0.0211 0.0189 0.0163 0.0131 0.0096 0.0059 0.0020 -0.0020 -0.0059 -0.0096 -0.0131 -0.0163 -0.0189 -0.0211 -0.0227 -0.0237 -0.0240 0.0240 0.0237 0.0227 0.0211 0.0189 0.0163 0.0131 0.0096 0.0059 0.0020 -0.0020 -0.0059 -0.0096 -0.0131 -0.0163 -0.0189 -0.0211 -0.0227 -0.0237 -0.0240 0.0240 0.0237 0.0227 0.0211 0.0189 0.0163 0.0131 0.0096 0.0059 0.0020 -0.0020 -0.0059 -0.0096 -0.0131 -0.0163 -0.0189 -0.0211 -0.0227 -0.0237 -0.0240 0.0240 0.0237 0.0227 0.0211 0.0189 0.0163 0.0131 0.0096 0.0059 0.0020 -0.0020 -0.0059 -0.0096 -0.0131 -0.0163 -0.0189 -0.0211 -0.0227 -0.0237 -0.0240 0.0240 0.0237 0.0227 0.0211 0.0189 0.0163 0.0131 0.0096 0.0059 0.0020 -0.0020 -0.0059 -0.0096 -0.0131 -0.0163 -0.0189 -0.0211 -0.0227 -0.0237 -0.0240
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
vy = sin(Beta) * j
vy = 100×20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0353 0.0353 0.0353 0.0353 0.0353 0.0353 0.0353 0.0353 0.0353 0.0353 0.0353 0.0353 0.0353 0.0353 0.0353 0.0353 0.0353 0.0353 0.0353 0.0353 0.0524 0.0524 0.0524 0.0524 0.0524 0.0524 0.0524 0.0524 0.0524 0.0524 0.0524 0.0524 0.0524 0.0524 0.0524 0.0524 0.0524 0.0524 0.0524 0.0524 0.0691 0.0691 0.0691 0.0691 0.0691 0.0691 0.0691 0.0691 0.0691 0.0691 0.0691 0.0691 0.0691 0.0691 0.0691 0.0691 0.0691 0.0691 0.0691 0.0691 0.0850 0.0850 0.0850 0.0850 0.0850 0.0850 0.0850 0.0850 0.0850 0.0850 0.0850 0.0850 0.0850 0.0850 0.0850 0.0850 0.0850 0.0850 0.0850 0.0850 0.1001 0.1001 0.1001 0.1001 0.1001 0.1001 0.1001 0.1001 0.1001 0.1001 0.1001 0.1001 0.1001 0.1001 0.1001 0.1001 0.1001 0.1001 0.1001 0.1001 0.1142 0.1142 0.1142 0.1142 0.1142 0.1142 0.1142 0.1142 0.1142 0.1142 0.1142 0.1142 0.1142 0.1142 0.1142 0.1142 0.1142 0.1142 0.1142 0.1142 0.1271 0.1271 0.1271 0.1271 0.1271 0.1271 0.1271 0.1271 0.1271 0.1271 0.1271 0.1271 0.1271 0.1271 0.1271 0.1271 0.1271 0.1271 0.1271 0.1271 0.1387 0.1387 0.1387 0.1387 0.1387 0.1387 0.1387 0.1387 0.1387 0.1387 0.1387 0.1387 0.1387 0.1387 0.1387 0.1387 0.1387 0.1387 0.1387 0.1387
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
.
  29 件のコメント
Noob
Noob 2025 年 1 月 3 日
編集済み: Noob 2025 年 1 月 3 日
I think I have good working surface plots / tiles now, and I'll take a much-needed break for now, before jumping back in to work on this stuff.
Thanks again for your time and help!
Star Strider
Star Strider 2025 年 1 月 3 日
With respect to filling in the patches, perhaps using mesh instead of surf will do what you want. (I’m stiill not sure what that is.)
Both zeros and NaN (or ones or rand or randn, etc.) will work for preallocation. The difference is that NaN values do not plot, and all the others, each having a finite value for every element, will plot.

サインインしてコメントする。

その他の回答 (1 件)

Sameer
Sameer 2025 年 1 月 2 日
編集済み: Sameer 2025 年 1 月 2 日
Hi
To solve your problem of writing a scalar-valued function using meshgrid and linear combinations of rotating basis vectors, you need to ensure that all operations are element-wise and that the dimensions match properly.
function v_norm = velocity_norm(alpha, beta, theta)
% rotating basis vectors
ip = [cos(theta); sin(theta); 0];
jp = [-sin(theta); cos(theta); 0];
kp = [0; 0; 1]; % Not used in this example, but defined for completeness
% Calculate velocity components
vx = cos(alpha) * ip(1) + sin(beta) * jp(1);
vy = cos(alpha) * ip(2) + sin(beta) * jp(2);
vz = cos(alpha) * ip(3) + sin(beta) * jp(3);
% Calculate the norm of the velocity vector
v_norm = sqrt(vx.^2 + vy.^2 + vz.^2);
end
alpha = linspace(0, pi, 20);
beta = linspace(0, 10, 100);
[Alpha, Beta] = meshgrid(alpha, beta);
% Define the angle theta
theta = 0;
% Calculate the velocity norm for each alpha and beta
V_norm = velocity_norm(Alpha, Beta, theta);
figure;
surf(Alpha, Beta, V_norm);
xlabel('\alpha');
ylabel('\beta');
zlabel('Velocity Norm');
title('3D Surface Plot of Velocity Norm');
Hope this helps!

タグ

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by