Efficiently Solving Diffusion-Based Equations Using Shooting Method in MATLAB

  • MATLAB
  • Thread starter baseball1234
  • Start date
  • Tags
    Method
In summary, the conversation discusses the equations attached for diffusion and the methods used to solve them, including the explicit finite difference method and the shooting method. The person speaking has developed a program in MATLAB to solve the equations, but is experiencing issues with "NaN" values in the solutions. They also provide their boundary and initial conditions, as well as their stability requirements for the method.
  • #1
baseball1234
3
0
Hi, I have the attached diffusion based equations I am looking to solve, however I am unsure about how to implement. I have solved similar equations before, with a simplified r(cG,cO) using the explicit finite difference method and it worked fine. However, I am wondering if that would be OK for this set of equations. I read that people used shooting method to solve these, but I am unfamiliar with this method. Can I also use the differential equation solvers in MATLAB to do this? Thanks.
 

Attachments

  • Equations.png
    Equations.png
    2.3 KB · Views: 462
Physics news on Phys.org
  • #2
Hi, I have developed a program in MATLAB to solve these equations, however I was wondering if someone can take a look for me. My solutions are all coming out to be "NaN", which is "not-a-number" in Matlab. I am using the explicit finite difference scheme to solve. Any suggestions would be great. My code below will run in MATLAB from just a copy/paste from here. My boundary conditions are the following:

Code:
x = 0: dcG/dx=0, dcO/dx=0 and cH = 0  
x = d: cG = cG0, cO = O20, cH = 0 
 
 Initial conditions are
 
 cH(t = 0) = 0
 cG(t = 0) =cG0
 cO(t = 0) = O20


Code:
clear all;

numx =50;                       %number of grid points in space
numt =82000;                    %number of time steps to be iterated over 
tmax = .06;

Length =50E-7;                  %length of grid
DG = 4E-10;                     %requirement DG(dt)/dx^2 < .5, cm^2/sec
DO = 5E-9;                      %requirement DO(dt)/dx^2 < .5, cm^2/sec
DH = 5E-9;                      %requirement DH(dt)/dx^2 < .5, cm^2/sec            

Vmax = 60E-6;                   %A/cm^2
KG = 33E-6;                     %mol/cm^3
KO = .2E-6;
G0 =2E-6;                       %mol/cm^3 initial substrate concentration
O20 = .12E-6;                   %mol/cm^3 inital oxygen concentration

cG = zeros(numx,1);              %initialize everything to zero
cH = zeros(numx,1);              %initialize everything to zero
cO = zeros(numx,1);

zcG = linspace(0,Length,numx)';   %vector of x values, to be used for plotting
tG = linspace(0,tmax,numt)';      %vector of t values, to be used for plotting

zcO = linspace(0,Length,numx)';   %vector of x values, to be used for plotting
tO = linspace(0,tmax,numt)';      %vector of t values, to be used for plotting

zcH = linspace(0,Length,numx)';   %vector of x values, to be used for plotting
tH = linspace(0,tmax,numt)';      %vector of t values, to be used for plotting

dxG = zcG(2)-zcG(1);              %Define grid spacing in time
dtG = tG(2)-tG(1);                %Define grid spacing in time

dxO = zcO(2)-zcO(1);              %Define grid spacing in time
dtO = tO(2)-tO(1);                %Define grid spacing in time

dxH = zcH(2)-zcH(1);              %Define grid spacing in time
dtH = tH(2)-tH(1);                %Define grid spacing in time

S_Stability1 = (DG*dtG)/((dxG)^2) %check requirement DG(dtG)/dxG^2 < .5, cm^2/sec
O_Stability1 = (DO*dtO)/((dxO)^2) %check requirement DO(dtO)/dxO^2 < .5, cm^2/sec
H_Stability1 = (DH*dtH)/((dxH)^2) %check requirement DH(dtH)/dxH^2 < .5
        
%{ 
Dirchelet Boundary Conditions, where Neumann BC dcG/dx=0 and dcO/dx=0
is in body of for loop below, IC is given by matrix initialization (zeros) and BC
%}

cG(numx) = G0;                 %cG(x=d,t)=G0
cO(numx) = O20;                %cO(x=d,t)=O20
cH(1) = 0;                     %cH(x=0,t)=0
cH(numx) = 0;                  %cH(x=d,t)=0

%iterate central difference equation% 

for j=1:numt %time loop  
    
           cGlast = cG;
           cOlast = cO;
           cHlast = cH;
           
      
    for i=2:numx-1 %space loop
              
      cG(i) = cGlast(i) + (dtG/dxG^2)*DG*(cGlast(i+1) - 2*cGlast(i) + cGlast(i-1))-((Vmax*dtG*cGlast(i)*cOlast(i))/(cGlast(i)*cOlast(i)+(KG*cOlast(i))+KO*cGlast(i))); 
      cO(i) = cOlast(i) + (dtO/dxO^2)*DO*(cOlast(i+1) - 2*cOlast(i) + cOlast(i-1))-((Vmax*dtO*cGlast(i)*cOlast(i))/(cGlast(i)*cOlast(i)+(KG*cOlast(i))+KO*cGlast(i)));
      cH(i) = cHlast(i) + (dtH/dxH^2)*DH*(cHlast(i+1) - 2*cHlast(i) + cHlast(i-1))+((Vmax*dtH*cGlast(i)*cOlast(i))/(cGlast(i)*cOlast(i)+(KG*cOlast(i))+KO*cGlast(i)));
      
      y(j,1) = cH(2);  %store data in vector for current plotting 
      
    end
    
    cG(1)= cGlast(1)+dtG*DG*2*(cGlast(2)-cGlast(1))./dxG.^2; %Neumann Boundary Condition
    cO(1)= cOlast(1)+dtO*DO*2*(cOlast(2)-cOlast(1))./dxO.^2; %Neumann Boundary Condition 
    
end

figure(1);
plot(zcH,cH);
xlabel('Length (cm)');
ylabel('[H2O2] (M)');

figure(2);
plot(zcG,cG);
xlabel('Length (cm)');
ylabel('[Glucose] (M)');


figure(3);
plot(zcO,cO);
xlabel('Length (cm)');
ylabel('[O2] (M)');

figure(4);   
i = 2*(96500)*DH*(y/dxH);
%i = 2*(96500)*DH*(y/dxH);
plot(tH,i);
xlabel('Time (s)');
ylabel('Current (A)');
 

1. What is the scientific method and how does it help solve problems?

The scientific method is a systematic approach used by scientists to solve problems or answer questions about the natural world. It involves making observations, formulating a hypothesis, conducting experiments, and analyzing data to draw conclusions. This method helps to ensure that results are reliable and can be replicated by other scientists, leading to a better understanding of the world around us.

2. What are some common methods used to solve scientific problems?

Some common methods used to solve scientific problems include experimentation, observation, modeling, and data analysis. These methods often involve testing hypotheses, collecting and analyzing data, and making observations in order to draw conclusions and solve the problem at hand.

3. How do scientists choose which method to use to solve a particular problem?

Scientists choose a method based on the specific problem they are trying to solve and the type of data they need to collect. For example, if the problem involves understanding the behavior of a particular organism, observation and experimentation may be the most appropriate methods. If the problem involves understanding complex systems, modeling and data analysis methods may be more effective.

4. Can more than one method be used to solve a scientific problem?

Yes, in fact, it is common for multiple methods to be used in combination to solve a scientific problem. For example, a scientist may use both experimentation and data analysis to test a hypothesis and draw conclusions. By using multiple methods, scientists can gain a more comprehensive understanding of the problem and increase the reliability of their results.

5. How does the choice of method affect the outcome of a scientific study?

The choice of method can greatly affect the outcome of a scientific study. For example, using a flawed or biased method can lead to inaccurate results and conclusions. Additionally, some methods may be better suited for certain types of problems or data, so choosing the most appropriate method can greatly impact the accuracy and reliability of the study's findings.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Calculus and Beyond Homework Help
Replies
10
Views
471
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
3K
Replies
4
Views
754
Back
Top