Can you explain what this matlab program is doing. I am new to matlab and i need
ID: 703990 • Letter: C
Question
Can you explain what this matlab program is doing. I am new to matlab and i need to know each and every step. Both the files are for the same problem. This question is from chemical engineering reaction class.
Editor-abdrive.tntech.edulabdrivelnspatel421DocumentstMATLABNILESH NILESH_4210HW2FIG43.m XNILESH_4210HW2FIG4_3graph.mX+ function dCdt-ilLESH_4210Hw2 FIG4-3 (t, conc, k) &Material; Balance =File name (x-axis, y-axis) % t will be defined CA-conc (1); CB-conc ( 2 ); dCdt (1)--k* CA; by the code 5- 6- % % mol/m^ 3 mol/m^ 3 %Material Balance for A 8- dCdt (2)-k*CA; %Material Balance for B 9--dCdt=dCdt'; transposing output matrixExplanation / Answer
Script 1 - At the top: NILESH…4_3
Line 1: Creates a function of name NILESH_... which returns dCdt ( an array) and has parameters of t, conc and k.
Lines 2,3 and 4 are commented and just explain what the code does. They have no role in execution.
Line 5 and 6: Creates a horizontal array conc, whose first element is CA and second element is CB.
Lines 7 and 8: Creates another array dCdt whose first and second elements give the rate law. With respect to rate laws, dCdt is the element wise differential of vector C, but it doesn’t mean so in the code. dCdt is just an independent matrix with elements –k*CA and k*CA.
Line 9: Making the horizontal array into a column vector by taking transpose.
Script 2 – At bottom: NILESH … graph
Since Line numbers are not mentioned, I’ll explain according to lines so that it would be easy.
Line 1: clear all – Removes all existing variables; close all – closes all tabs/graphs; clc- clears command window
Line 2: Just explanation commented, no role
Line 3: Defining the value of rate constant as 0.5. MATLAB doesn’t know units so be careful.
Line 4: This is the syntax for the ode15 solver.
[time,y]=ode15s(@function,[time domain],[initial values])
we create a temporary function using the above script. Basically, the function gives values for dCdt i.e. the rate laws. Time domain is from 0 to 5 seconds. From initial values, CA(0)=1 and CB(0)=0.
This command will solve the set of differential equations and make an array of t and C containing the concentration at different times for both A and B.
Line 5: Explaining the syntax, commented
Line 6: hold on is used when we want to add the current graph over the existing graph i.e. adding two or more graphs
Line 7 onwards : The same thing is repeated for different values of rate constants k=1, k=2, k=5;
There are different sets of soltions:
[t,C], [t1,C1], [t2,C2] and [t3,C3]
plot – Plots all of these graphs on one graph with time on X-axis and concentration C on Y-axis.
C(:,1) means all rows and column 1 i.e. it plots only CA since that is saved in column 1.
xlabel – Adds label to the X-axis
ylabel – Adds label to the Y-axis
legend – Legend for different plots, names them according to rate constants
------
I hope this was helpful explanation. For more doubts, you can ask me. Also, typing
>> help : and then selecting the desired command explains what it does on MATLAB.
If you liked the explanation, please consider giving a good review. Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.