Tutorial 04
Tutorial 04
– fun(1,:)=krs*Y(2)-kfs*Y(1)*Y(3)-kfp*Y(1); %dCls/dt
– fun(2,:)=kfs*Y(1)*Y(3)-krs*Y(2)+krec*Y(5)-kec*Y(2);
%dCcs/dt
– fun(3,:)=krs*Y(2)-kfs*Y(1)*Y(3); %dCrs/dt
– fun(4,:)=kfp*Y(1)+kri*Y(5)-kfi*Y(4)*Y(6)-kdeg*Y(4);
%dCli/dt
– fun(5,:)=kec*Y(2)+kfi*Y(4)*Y(6)-kri*Y(5)-krec*Y(5);
%dCci/dt
– fun(6,:)=kri*Y(5)-kfi*Y(4)*Y(6); %dCri/dt
– fun(7,:)=-kdeg*Y(4); %dCLtot/dt
Plotting Your Data
• After you have called your function (and
assigned a variable name to the sol’n)
– Type: figure (don’t need a semicolon here)
– Type: plot(t,X)
• t is your time vector and X is the sol’n vector that you
named in your function call or part of your sol’n
matrix (i.e. X(:,1), first column of matrix)
– Note: You can type semilogx(t,X) or
semilogy(t,X) to get a semilog plot of your
choosing
Plotting Your Data Continued..
• Typing “hold on” after introducing a second
figure will allow you to plot multiple curves
on the same set of axes
• Using the “subplot(x,y,z), plot(t,X)” sequence
will allow you to plot a matrix of graphs of
size (x,y) on the same page, with z being the
location of the graph in the matrix
• Typing “plot(t,X,’letter’)” will allow you to
control the color of the line for that plot, type
‘help plot” in prompt to see the color key
Labeling Axes,Making Legends
• For the plot title, x-axis, and y-axis, type:
– title(‘title’)
– xlabel(‘axis name’)
– ylabel(‘axis name’)
• To make a legend, type:
– legend(‘name of curve 1,’name of curve 2’, etc.)
• You can include variable values in these labels
– title([‘Binding isotherm with k = ’ num2str(k) ‘ s^-1’])
• If you are out of time, you can also use the “Insert
text” button (with an “A” on it) to label your plots
just before you print them. However, these labels
will not appear when you rerun the m-file.
Saving Your Work
• In the MATLAB prompt, type:
– save filename
• In Windows, just use the save icon or the save
option in the drop down menu under file
• Make sure that your file is saved in the proper
directory so that it can run from the MATLAB
prompt
• NLINFIT
– Allows you to do nonlinear curve-fitting
• FSOLVE
– Allows you to solve for unknowns in an algebraic
equation or in systems of algebraic equations
• Use the “help functionname” command to see the
proper syntax for setting up these types of
problems
Part III
HELP!!!
Some advice on getting help…
• USE THE HELP SEARCH TOOL
– In MATLAB 6.xx, type:
• help functionname
• Debug carefully
– Write your code a little at a time
– Use flags to see where errors are
• If debugging is going nowhere, ask a friend to
check things out
• If things are still stuck, come see the TAs
MIT Help
• Go to:
http://web.mit.edu/answers/www/matlab/
http://www.mathworks.com/