Creating 3D Plot using Surf
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
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
0 个评论
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Gamma Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!