I understand that you want to store a set of data into a TXT file and plot a 2D and 3D mesh using it. To achieve your goal, you need to store the calculated values in arrays so that you can plot them later. Your current code only writes to a file and attempts to plot using the last calculated myPerim and myArea, which won't work for plotting all the values. Here's a revised version of your code
% initialize arrays to store the values for plotting
perimeters = [];
areas = [];
x_vals = [];
y_vals = [];
hypots = [];
myFile = fopen('values.txt', 'w');
% format the file as stated in the question
fprintf(myFile, "x\t y\t h\t p\t a\n");
for i = 1:5
for j = 1:5
myHypot = hValue(i,j);
myPerim = pValue(i,j);
myArea = aValue(i,j);
% writing to the TXT file
fprintf(myFile, "%d\t\t\t%d\t\t\t%.2f\t\t\t%.2f\t\t\t%.2f\n", i, j, ...
myHypot, myPerim, myArea);
% store values for plotting
perimeters = [perimeters, myPerim];
areas = [areas, myArea];
hypots = [hypots, myHypot];
end
end
fclose(myFile);
% plot the 2D graph of perimeter vs. area
figure;
plot(perimeters, areas, 'o-');
xlabel('Perimeter');
ylabel('Area');
title('Perimeter vs. Area');
% plot the mesh with x, y, and hypotenuse
figure;
[X, Y] = meshgrid(1:5, 1:5);
mesh(X, Y, reshape(hypots, [5, 5]));
xlabel('X Length');
ylabel('Y Length');
zlabel('Hypotenuse Length');
title('Mesh plot of Hypotenuse Length');
% function to compute the hypotenuse
function hValue = hValue(xValue,yValue)
hValue = sqrt(xValue.^2 + yValue.^2);
end
% function to compute the perimeter
function pValue = pValue(xValue,yValue)
pValue = xValue + yValue + sqrt(xValue.^2 + yValue.^2);
end
% function to determine the area
function aValue = aValue(xValue,yValue)
aValue = 0.5 * xValue * yValue;
end
I hope this helps.