Issue with 3d plotting and writing to file

조회 수: 3 (최근 30일)
Mitul Dattani
Mitul Dattani 2018년 5월 16일
댓글: Mitul Dattani 2018년 5월 16일
Hi, I'm doing a revision question for uni exams, and have the question - https://gyazo.com/4c0a0357bb2ffb6f2526637442e09a91
My issue is with the last part of the final question, my data points are much higher that required in the question. The code I've written is:
z = 1-x.^2 - y.^2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,1,1)
contour3(x,y,z)
subplot(3,1,2)
surf(x,y,z)
subplot(3,1,3)
mesh(x,y,z)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
header1 = 'x';
header2 = 'y';
header3 = 'z';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid = fopen('mydata2.txt','w');
fprintf(fid,['\t', header1, '\t\t', header2, '\t\t', header3, '\n']);
for z = 1-x.^2 - y.^2
xx = -1:0.1:1;
yy = -2:0.1:2;
fprintf(fid, '%f %f %f \n',xx,yy,z);
end
fclose(fid);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Im pretty sure my loop is wrong or something to do with my axis? Any help is much appreciated!
Thanks
  댓글 수: 1
KSSV
KSSV 2018년 5월 16일
YOu can write whole data into text file using dlmwrite, save....

댓글을 달려면 로그인하십시오.

채택된 답변

Aditya Adhikary
Aditya Adhikary 2018년 5월 16일
Assuming that you need to get 20 coordinate points for x and 40 for y, you can use the meshgrid function to use as input for plotting in contour3, surf and mesh.
If you need to write a table for the (x,y,z) points, then the following code (modified from the above) might help:
x = linspace(-1,1,20);
y = linspace(-2,2,40);
[X,Y] = meshgrid(x,y);
Z = 1 - X.^2 - Y.^2;
subplot(3,1,1)
contour3(X,Y,Z,30)
subplot(3,1,2)
surf(X,Y,Z)
subplot(3,1,3)
mesh(X,Y,Z)
fid = fopen('mydata2.txt','w');
fprintf(fid,'%12s %12s %12s\r\n','x','y','z');
for i = 1:length(y)
for j = 1:length(x)
fprintf(fid, '%12.4f %12.4f %12.4f\r\n',X(i,j),Y(i,j),Z(i,j));
end
end
fclose(fid);
  댓글 수: 3
Aditya Adhikary
Aditya Adhikary 2018년 5월 16일
I assumed that 'data points' (as given in the question in the attached image) refers to the range of values that x and y can take. Hence, there are a total of 20*40 = 800 points (you take every possible pair of values from x and y).
Mitul Dattani
Mitul Dattani 2018년 5월 16일
ohh that makes alot more sense! we dont get questions answered based on past paper questions unfortunately so I just assumed it wanted 20 values for x and 40 for y in the text file but that way makes alot more sense! thanks again

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by