Finite Difference Iteration Code Issues

6 views (last 30 days)
H
H on 8 Feb 2014
Commented: H on 10 Feb 2014
I'm taking a groundwater course and the prof assumes that we know how to use matlab. I do not. He teaches material related to groundwater in class, and then tells us to model it in matlab, which he does not teach. My inability to use matlab is going to seriously impact how much of the material I learn in the class. I purchased the student version but have not been having much luck learning on my own. It is a simple finite difference method that we have to run until conversion. I have solved the problem already in excel but doing all of my assignments in excel is not going to help me learn to use matlab.
I would really appreciate if someone could help me out. In addition to solving, we also have to make a plot of the solution for C versus y at specified x values and also a table of C versus x at specified y values. I haven't even attempted that part yet.
clear;
%Define Parameters
r=0.5;
dt=16*r/0.119;
n=7;
t=60;
%Initial Value of Function C
for i=2:n-2
for j=2:100
c(i,j-1)=0.2
end
%Value at boundary
for j=1:100
c(1,j)=0.;
c(n-1,j)=0.1;
end
%implement explicit method
for j=1:100
for i=1:n
c(i,j+1)=c(i,j)+r*(c(i+1,j)-2*c(i,j)+c(i-1,j));
end
clast=c %save the last guess
err(c)=max(abs(c(:)-clast(:)));
if err(C)<0.001
break
end
  9 Comments
Roger Stafford
Roger Stafford on 9 Feb 2014
Yes it makes sense but that is not what you achieved by writing "clast=c". The effect of that line is, as I have said, to copy the entire 'c' matrix with all its 100 columns over to 'clast'. I think what you want is:
clast = c(:,j);
cpresent = c(:,j+1);
if max(abs(clast-cpresent))<0.001
break
end
H
H on 10 Feb 2014
Everything seems to be functioning for me now with the exception of the break. I expected that if the if statement was met at say column 32 it would only display the matrix with 32 columns rather than continue to 100. However, it continues to display results up to column 100.
Is that what is supposed to happen?

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!