How to make a table for two different value but depending on third single value?
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
If i have a function of A= Bx+zC
then i want a table of x and A for each value of z. For diffrent value of z in one file only with diffrent table.
p= 1;B=0.5;C=0.3
for z= [0 1 2 3]
x=0:0.1:p;
A=B.*x+z*C;
pp = fopen('A.txt','w');
fprintf(pp,'%6s %6s %12s\n','z','x','A');
fprintf(pp,'%6.2f %6.2f %12.8f\n',A);
fclose(pp);
end
I got out put which is not to my mark .
z x A
0.90 0.95 1.00000000
1.05 1.10 1.15000000
1.20 1.25 1.30000000
1.35 1.40
0 Kommentare
Antworten (1)
Arjun
am 27 Mär. 2025
I understand that you want table of 'x' and 'A' for every value of 'z' in a single ".TXT" file.
The code has the following potential issues: it opens the file in write mode ('w') during each loop iteration, which causes it to overwrite existing data, leaving only the last table in the file. Furthermore, the code does not iterate over 'x' values to correctly pair each 'x' with its corresponding 'A' value in the output.
To fix these issues, the file should be opened in append mode ('a') to ensure that each table is added sequentially without overwriting previous data. The code should also iterate over the 'x' values, printing each x-A pair on a separate line. The 'z' value should be included only in the header to provide context for each table.
Kindly refer to the following code:
p = 1;
B = 0.5;
C = 0.3;
% Open the file in append mode
pp = fopen('A.txt', 'a');
for z = [0 1 2 3]
x = 0:0.1:p;
A = B .* x + z * C;
% Write a header for each new table indicating the value of z
fprintf(pp, 'Table for z = %d\n', z);
fprintf(pp, '%6s %12s\n', 'x', 'A');
% Write the values of x and A
for i = 1:length(x)
fprintf(pp, '%6.2f %12.8f\n', x(i), A(i));
end
% Add a blank line for separation between tables
fprintf(pp, '\n');
end
fclose(pp);
Read about the different modes in which a file can be opened in this documentation link: https://www.mathworks.com/help/releases/R2021a/matlab/ref/fopen.html?searchHighlight=fopen&searchResultIndex=1#btrnibn-1-permission
I hope this helps!
Siehe auch
Kategorien
Mehr zu Polynomials finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!