Creating 3D Plot using Surf

조회 수: 16 (최근 30일)
onsagerian
onsagerian 2019년 11월 7일
답변: Star Strider 2019년 11월 7일
Hello,
I am trying to create a plot in 3D using "surf" command with specific conditions. I would like to plot "g" depending on two variables, the value of "gamma" and "f" with speicfic value of "n". The 1D array of gamma is defined as follows, and also I need to see the result (the value of "g") only when n=3. I have tried to modify my program in several ways such as using "if" statement, but they do not work. Basically, the following code produces a plot for all possible values of "n" (from 1 to 5), but what I need to obtain is "g" that depends on "gamma" and "f" when "n=3". Would you hellp me to design the program code fulfilling the two conditions?
format long e
n=1:1:5;
m=1:1:10;
alpha=0.01;
A=zeros(length(n),length(m));
B=zeros(length(n),length(m));
f=zeros(length(n),length(m));
g=zeros(length(n),length(m));
gamma=zeros(length(m));
for i=1:1:length(n)
for j=1:1:length(m)
gamma(j)=10^(j-1);
A(i,j)=i.^2/j.^3;
B(i,j)=A(i,j)./(alpha+i);
f(i,j)=B(i,j)./(A(i,j)+i);
g(i,j)=A(i,j)./j;
%fprintf('%d %.10e %.10e %.10e\n',i,gamma(j),f(i,j),g(i,j));
end
%fprintf('\n');
end
for i=1:1:length(n)
for j=1:1:length(m)
[X,Y]=meshgrid(1:length(n),1:length(m));
if(i==3) <===== % Does not work!
surf(Y,f',g'); <===== % How can I incorporate "gamma" rather than "Y"?
xlabel('gamma');
ylabel('f');
zlabel('g');
end
end
end

답변 (2개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 11월 7일
편집: KALYAN ACHARJYA 2019년 11월 7일
format long e
n=1:1:5;
m=1:1:10;
alpha=0.01;
A=zeros(length(n),length(m));
B=zeros(length(n),length(m));
f=zeros(length(n),length(m));
g=zeros(length(n),length(m));
gamma=zeros(length(m));
for i=1:1:length(n)
for j=1:1:length(m)
gamma(j)=10^(j-1);
A(i,j)=i.^2/j.^3;
B(i,j)=A(i,j)./(alpha+i);
f(i,j)=B(i,j)./(A(i,j)+i);
g(i,j)=A(i,j)./j;
%fprintf('%d %.10e %.10e %.10e\n',i,gamma(j),f(i,j),g(i,j));
end
%fprintf('\n');
end
for i=1:1:length(n)
for j=1:1:length(m)
[X,Y]=meshgrid(1:length(n),1:length(m));
if i==3
surf(Y',f,g);
xlabel('gamma');
ylabel('f');
zlabel('g');
end
end
end
567.png

Star Strider
Star Strider 2019년 11월 7일
One possibility is to preallocate ‘gamma’ as a vector, since it appears to be such in the rest of your code:
gamma = zeros(size(m));
then create it as a matrix in your surf call:
surf((ones(5,1)*gamma)',f',g')
If you want ‘gamma’ to be a matrix of different row elements, you will need to assign it as such, since at present, you are assigning it as a vector. My version of your surf call simply creates it as a matrix of identical rows.

카테고리

Help CenterFile Exchange에서 Gamma Functions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by