Tabulate data from the mathlab program

  • Thread starter Thread starter iwan89
  • Start date Start date
  • Tags Tags
    Data Program
AI Thread Summary
The discussion focuses on how to tabulate data from a MATLAB program that computes temperature over time and space. The user seeks assistance in modifying their code to print the data in a two-column table format, representing u(x,t) versus x. Clarification is provided on the format specifier used in the fprintf function, explaining how it controls the output's appearance. The conversation also addresses the need for proper formatting to ensure the data is displayed correctly. Overall, the thread emphasizes the importance of clear data presentation in MATLAB.
iwan89
Messages
27
Reaction score
0
I need to tabulate the data for u(x,t) vs x. Someone help me please :(
this is the program.

L = 1.;
T = 1.;
maxk = 2500;
dt = T/maxk;
n=50;
dx = L/n;
cond = 1/4;
b = 2.*cond*dt/(dx*dx);
for i= 1:n+1
x(i) = (i-1)*dx;
u(i,1) = sin(pi*x(i));
end
for k=1:maxk+1
u(1,k) = 0.;
u(n+1,k)=0.;
time(k) = (k-1)*dt;
end
for k=1:maxk
for i=2:n;
u(i,k+1)=u(i,k)+0.5*b*(u(i-1,k)+u(i+1,k)-2.*u(i,k));
end
end
figure (1)
plot (x,u(:,1),'-',x,u(:,100),'-',x,u(:,300),'-',x,u(:,600),'-')
title ('Temperature within the explicit method')
xlabel ('X')
ylabel ('Y')
figure (2)
mesh (x,time,u')
title ('Temperature within the explicit method')
xlabel ('X')
ylabel ('Temperature')
 
Physics news on Phys.org
iwan89 said:
I need to tabulate the data for u(x,t) vs x. Someone help me please :(
What do you mean? Do you need to print the data in a table? If so, how many columns? What should the table look like?

BTW, this is not the section for HW problems. I am moving this post back to where your other one is.
iwan89 said:
this is the program.

L = 1.;
T = 1.;
maxk = 2500;
dt = T/maxk;
n=50;
dx = L/n;
cond = 1/4;
b = 2.*cond*dt/(dx*dx);
for i= 1:n+1
x(i) = (i-1)*dx;
u(i,1) = sin(pi*x(i));
end
for k=1:maxk+1
u(1,k) = 0.;
u(n+1,k)=0.;
time(k) = (k-1)*dt;
end
for k=1:maxk
for i=2:n;
u(i,k+1)=u(i,k)+0.5*b*(u(i-1,k)+u(i+1,k)-2.*u(i,k));
end
end
figure (1)
plot (x,u(:,1),'-',x,u(:,100),'-',x,u(:,300),'-',x,u(:,600),'-')
title ('Temperature within the explicit method')
xlabel ('X')
ylabel ('Y')
figure (2)
mesh (x,time,u')
title ('Temperature within the explicit method')
xlabel ('X')
ylabel ('Temperature')
 
yes i want to print the data and put it inside the table. two colomn which represent the graph
 
Im in total lost :( can you help me to modify my program so that it can create a table? :(
 
Something like this. I don't know if this is exactly what you need, but it's probably close.
Code:
formatSpec = "%4.2f \t %4.2f \n";
for k=1:maxk
  for i=2:n;
    u(i,k+1)=u(i,k)+0.5*b*(u(i-1,k)+u(i+1,k)-2.*u(i,k));
    fprintf(formatSpec, x(i), u(i, k + 1))
  end
end
 
what is %4.2f \t %4.2f \n meant?
 
%4.2f is a format specifier - print a number in a field of width 4, with 2 places to the right of the decimal point. If you have numbers that take up more space, change the 4 to something bigger. If you want more places to the right of the decimal, change the 2 to something bigger.

\t is a tab character
\n is a newline character.
 

Similar threads

Replies
1
Views
2K
Replies
10
Views
2K
Replies
6
Views
3K
Replies
7
Views
1K
Replies
10
Views
3K
Replies
6
Views
5K
Replies
8
Views
2K
Back
Top