Plotting a 3d figure for a function with 4 variables?
이전 댓글 표시
Hi, I have a function with 4 variables (x,y,z,w) which means d(x,y,z,w)=0, and I have to plot a 3D figure for just 3 variable (x,y,z) without specific values. could you please tell me which matlab function is useful?
here is my code:
function [] = critical()
[x,y,z] = meshgrid(-4:0.1:4,-4:0.1:4,-4:0.1:4);
w=1;
%parameter
a=25;K=0.5;K_p=0.4;K_b=0.3; V_s=0.8;K_s=0.2; gamma=6;K_bar=1.5e-4;
%Function
phi_1 = y.^3 ./ ( y.^3 + K ^3 );
phi_2 = w.^2 ./ ( K_p^2 + w.^2);
phi_3= K_p^2 ./ ( K_p^2 + w.^2);
teta = phi_1 .* phi_2 .* z;
aa = phi_3 .* ( 1 - phi_3).*x;
b = teta./(teta+K_b*(teta+aa));
Beta=(1-K_bar*(gamma*(x-y)./y).^2)./(1+(K_s./y).^2);
alpha=a*b*gamma.*(x-(1+(1/gamma).*y))/V_s;
d=alpha-Beta;
fv=isosurface(x,y,z,d,0)
fv = isosurface(d,0)
fvc = isosurface(d,'blue')
pp = patch(isosurface(x,y,z,d,-0))
isonormals(x,y,z,d,pp)
set(pp,'FaceColor','red','EdgeColor','none');
view(3);
axis tight
camlight
end
when I define w as a vector [w]=meshgrid(-4:0.1:4); it has this error:
Error using .*
Matrix dimensions must agree.
Error in criticalaki (line 11)
teta = phi_1 .* phi_2 .* z;
so I put w=1 which is not true and give me the figure below. but I want a soft figure like surface.

댓글 수: 15
darova
2021년 8월 14일
Are your x,y,z matrices 3D?
Adam Danz
2021년 8월 14일
> ... but it doesn't give me a nice figure
What isn't nice about it? How does it not address your goal? Is the problem associated with the creation of patch values (i.e. your use of isosurface) or is the problem with plotting the values? We don't have sufficient information to address the question and the question itself is fuzzy.
Clearly state the goal, the problem you're having achieving the goal, and supply any data and code needed to reproduce the problem if necessary.
darova
2021년 8월 15일
Do you have original formulas for w?
M
2021년 8월 15일
Your code is copied below so that Matlab Central's Run Feature can produce the plot. I also added 4 semicolons to suppress unnecessary outputs. Lastly, I added the line you described to define w with meshgrid() but I do not get that error.
[x,y,z] = meshgrid(-4:0.1:4,-4:0.1:4,-4:0.1:4);
% w=1;
[w]=meshgrid(-4:0.1:4)
%parameter
a=25;K=0.5;K_p=0.4;K_b=0.3; V_s=0.8;K_s=0.2; gamma=6;K_bar=1.5e-4;
%Function
phi_1 = y.^3 ./ ( y.^3 + K ^3 );
phi_2 = w.^2 ./ ( K_p^2 + w.^2);
phi_3= K_p^2 ./ ( K_p^2 + w.^2);
teta = phi_1 .* phi_2 .* z;
aa = phi_3 .* ( 1 - phi_3).*x;
b = teta./(teta+K_b*(teta+aa));
Beta=(1-K_bar*(gamma*(x-y)./y).^2)./(1+(K_s./y).^2);
alpha=a*b*gamma.*(x-(1+(1/gamma).*y))/V_s;
d=alpha-Beta;
fv=isosurface(x,y,z,d,0);
fv = isosurface(d,0);
fvc = isosurface(d,'blue');
pp = patch(isosurface(x,y,z,d,-0));
isonormals(x,y,z,d,pp)
set(pp,'FaceColor','red','EdgeColor','none');
view(3);
axis tight
camlight
M
2021년 8월 16일
Adam Danz
2021년 8월 16일
What Matlab release are you using?
phi_1 and z should be size 81x81x81, phi_2 sould be 81x81.
M
2021년 8월 17일
Adam Danz
2021년 8월 18일
I see. The problem must be due to your older release, then. Explicit expansion of variables is supported from R2016b onward.
I am not familiar enough with your data to recommend an alternative to isosurface.
This should at least fix your errors.
%teta = phi_1 .* phi_2 .* z;
teta = phi_1 .* bsxfun(@times,phi_2,z);
%aa = phi_3 .* ( 1 - phi_3).*x;
aa = bsxfun(@times,phi_3 .* ( 1 - phi_3),x);
isosurface doesn't tend to give a very pretty figure for a coarse grid. In order to use surf(), you'd need to express (either literally or mathematically) the location of the isosurface as a mesh of points in x,y,z space. It might look a lot nicer than using isosurface(), but it's often a lot more complicated to accomplish.
FWIW, you can always throw extra memory at it and use a finer mesh. I don't have much memory, so I just did this:
vv = linspace(-4,4,400);
[x,y,z] = meshgrid(single(vv));
[w]=meshgrid(vv);
%parameter
a=25;K=0.5;K_p=0.4;K_b=0.3; V_s=0.8;K_s=0.2; gamma=6;K_bar=1.5e-4;
%Function
phi_1 = y.^3 ./ ( y.^3 + K ^3 );
phi_2 = w.^2 ./ ( K_p^2 + w.^2);
phi_3= K_p^2 ./ ( K_p^2 + w.^2);
teta = phi_1 .* bsxfun(@times,phi_2,z);
aa = bsxfun(@times,phi_3 .* ( 1 - phi_3),x);
clear phi_1 phi_2 phi_3 w
b = teta./(teta+K_b*(teta+aa));
Beta=(1-K_bar*(gamma*(x-y)./y).^2)./(1+(K_s./y).^2);
alpha=a*b*gamma.*(x-(1+(1/gamma).*y))/V_s;
d=alpha-Beta;
clear alpha Beta a b aa teta
pp = patch(isosurface(x,y,z,d,-0));
isonormals(x,y,z,d,pp)
set(pp,'FaceColor','red','EdgeColor','none');
view(3);
axis tight
camlight
grid on

It's a bit better, but it's still rough. At this resolution, each one of the 3D arrays occupies about 250MB, so it gets impractical to do much more.
Adam Danz
2021년 8월 19일
+1 DGM
Move to answer?
DGM
2021년 8월 19일
I kind of assumed that the core of the question was "how to I rewrite this to make a surf plot", and I really left that unanswered.
M
2021년 8월 19일
DGM
2021년 8월 19일
If you're running out of memory, with that example, then you'd need to reduce the number of points to something less than 400.
If you managed to get it working with surf, then you probably don't need to bother with my example.
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
