November 26, 2024, 12:44:54 PM
Forum Rules: Read This Before Posting


Topic: Using Euler's method to make a plot of PA vs time for a reaction  (Read 6191 times)

0 Members and 1 Guest are viewing this topic.

Offline limonade

  • Regular Member
  • ***
  • Posts: 89
  • Mole Snacks: +4/-2
Hi there.


I am trying to use MATLAB to make a plot of PA vs time for the problem ( see picture #1)



I am able to get a component balance in terms of conversion, but when I integrate and use MATLAB to plot the conversion vs time I get the result = infinity for conversion. I do not know what is going wrong with my code.


I have attached the problem statement, my work on how I arrived at the equations, and my MATLAB code.

I am really really stuck here and would appreciate any help...




The code is here

clear all
clc

k = 1*10^-3;
KA = 10;
W = 0.1;
T = 400;
P = 100;
yA0 = 0.5;
yB0 = 0.5;
NA0 = .5       % Assuming a basis of 1 mol to start
Ptot = 100;
PA0 = 50;
V = 100,000; %cm3
R = 8.314*10^3 %units cm3 kPA K-1mol-1
i = 1; % initial array index
x(i) = 0; % dimensionless, initial conversion
t(i) = 0; % s, initial time
dt = 0.5; % s, step size
trxn = 5000; % s, final time

while t(i) <= trxn
dxdt = W*k*((PA0*(1-x)).^2)/(1+KA*(PA0*(1-x))*(PA0*V/R*T));
x(i+1) = x(i) + dxdt * dt
t(i+1) = t(i) + dt;
i = i+1; % increment array index
end

plot(t,x)
tt = sprintf('Conversion vs. time, X is %4.4f at trxn = %4.2f', max(x),trxn)
title(tt)
ylabel('X'), xlabel('time (s)')
axis([0 ceil(max(t)) 0 1])
max(x)

Offline limonade

  • Regular Member
  • ***
  • Posts: 89
  • Mole Snacks: +4/-2
Re: Using Euler's method to make a plot of PA vs time for a reaction
« Reply #1 on: April 10, 2014, 08:50:33 AM »
The "correct answer"/ resulting graph of partial pressure of A vs time should end up looking like this:






I just wanted to make sure that my conversion vs time graph comes out okay...


then I was going to make a while loop or something with an equation for pA in terms of Xa to get the graph asked in the problem statement.

Offline mjc123

  • Chemist
  • Sr. Member
  • *
  • Posts: 2071
  • Mole Snacks: +302/-12
Re: Using Euler's method to make a plot of PA vs time for a reaction
« Reply #2 on: April 10, 2014, 08:54:59 AM »
There appears to be a discrepancty in the denominator of your dx/dt expression. Your code has
(1+KA*(PA0*(1-x))*(PA0*V/R*T))
while your manuscript has
((1+KA*PA0*(1-x))*(PA0*V/R*T))
Be careful with those brackets!

Offline limonade

  • Regular Member
  • ***
  • Posts: 89
  • Mole Snacks: +4/-2
Re: Using Euler's method to make a plot of PA vs time for a reaction
« Reply #3 on: April 10, 2014, 09:55:56 AM »
I double checked with the brackets, and it still is not coming out properly.

Offline Corribus

  • Chemist
  • Sr. Member
  • *
  • Posts: 3551
  • Mole Snacks: +546/-23
  • Gender: Male
  • A lover of spectroscopy and chocolate.
Re: Using Euler's method to make a plot of PA vs time for a reaction
« Reply #4 on: April 10, 2014, 12:09:16 PM »
Many programs will interpret the "T" in that expression to be in the numerator of the latter part of the expression, that is, PAO*V*T/R.

Therefore, I suggest making sure you have a set of brackets around R and T uniquely.

I.e., something like:

dxdt = W*k*((PA0*(1-x)).^2)/((1+KA*PA0*(1-x))*(PA0*V/(R*T)));

This has still got some superfluous brackets in it, but it's ok I think. I assume all other syntax is correct - not sure what the . is for.
What men are poets who can speak of Jupiter if he were like a man, but if he is an immense spinning sphere of methane and ammonia must be silent?  - Richard P. Feynman

Offline curiouscat

  • Chemist
  • Sr. Member
  • *
  • Posts: 3006
  • Mole Snacks: +121/-35
Re: Using Euler's method to make a plot of PA vs time for a reaction
« Reply #5 on: April 10, 2014, 02:46:02 PM »
Have you tried with a smaller step size?

Offline limonade

  • Regular Member
  • ***
  • Posts: 89
  • Mole Snacks: +4/-2
Re: Using Euler's method to make a plot of PA vs time for a reaction
« Reply #6 on: April 10, 2014, 09:49:48 PM »
Thank you all very much for your replies. I figured it out.


For the volume, I put V = 100,000 cm3


It turns out that my MATLAB program interprets , as decimal points.



So.... 100,000 is read as 100.000


*sigh* terrible thing to spend so much time on it and not get the correct answer because of that


But now it works


Thanks again

Offline mjc123

  • Chemist
  • Sr. Member
  • *
  • Posts: 2071
  • Mole Snacks: +302/-12
Re: Using Euler's method to make a plot of PA vs time for a reaction
« Reply #7 on: April 11, 2014, 04:46:37 AM »
You might fiind it worth creating some intermediate variables to make the algebra easier. For example
Y = PA0*V/(R*T)
Z = PA0*(1-x)
Then dx/dt = W*k*Z^2/((1+KA*Z)*Y)
Much less chance of accidental typos.

Sponsored Links