Tabulate data from the mathlab program

  • Thread starter Thread starter iwan89
  • Start date Start date
  • Tags Tags
    Data Program
Click For Summary

Discussion Overview

The discussion revolves around how to tabulate data generated from a MATLAB program that computes the function u(x,t) over a specified range. Participants are seeking assistance in modifying the existing code to output the data in a tabular format, focusing on the relationship between u(x,t) and x.

Discussion Character

  • Homework-related, Technical explanation, Exploratory

Main Points Raised

  • One participant requests help to tabulate data for u(x,t) vs x, indicating a need for clarity on how to format the output.
  • Another participant seeks clarification on the desired structure of the table, asking about the number of columns and the overall appearance.
  • A participant expresses confusion and requests modifications to the program to create the table.
  • A suggestion is made to use the fprintf function in MATLAB to format the output, with an example format specifier provided.
  • Further clarification is given regarding the meaning of the format specifier, explaining how it controls the output format of the numbers.

Areas of Agreement / Disagreement

Participants generally agree on the need to format the output as a table, but there is no consensus on the exact implementation details or the final structure of the table.

Contextual Notes

There are unresolved questions regarding the specific format and structure of the table, as well as the potential need for adjustments based on the data's characteristics.

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 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K