Effacer les filtres
Effacer les filtres

Issue with 3d plotting and writing to file

1 vue (au cours des 30 derniers jours)
Mitul Dattani
Mitul Dattani le 16 Mai 2018
Commenté : Mitul Dattani le 16 Mai 2018
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 commentaire
KSSV
KSSV le 16 Mai 2018
YOu can write whole data into text file using dlmwrite, save....

Connectez-vous pour commenter.

Réponse acceptée

Aditya Adhikary
Aditya Adhikary le 16 Mai 2018
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 commentaires
Aditya Adhikary
Aditya Adhikary le 16 Mai 2018
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 le 16 Mai 2018
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

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by