• Support PF! Buy your school textbooks, materials and every day products Here!

Tabulate data from the mathlab program

  • Thread starter iwan89
  • Start date
  • #1
27
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')
 

Answers and Replies

  • #2
33,501
5,190
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.
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')
 
  • #3
27
0
yes i want to print the data and put it inside the table. two colomn which represent the graph
 
  • #5
27
0
Im in total lost :( can you help me to modify my program so that it can create a table? :(
 
  • #6
33,501
5,190
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
 
  • #7
27
0
what is %4.2f \t %4.2f \n meant?
 
  • #8
33,501
5,190
%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.
 

Related Threads on Tabulate data from the mathlab program

  • Last Post
Replies
0
Views
728
Replies
4
Views
2K
  • Last Post
Replies
1
Views
2K
Replies
3
Views
1K
Replies
4
Views
686
Replies
14
Views
19K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
14
Views
5K
Replies
3
Views
1K
Top