Matlab usage - short (may be too short) introduction by EugeniyMikhailov %!includeconf: ../configs/config.t2t %!style(html): ../evmik.css = Why Matlab = At the very least, it is a great replacement of a hand held calculator. Once you need to process multiple data point it became tedious with a simple calculator. Once you master Matlab it will not matter if it is one data point or millions of them. It can do complex math, plotting, fitting, etc. It is also the complete programming language which can stand by itself. Also, Matlab has excellent help documentation and a lot of tutorials at the web. There are free alternatives. [Octave http://www.gnu.org/software/octave/] is one example. This what I personally use instead of Matlab all the time. But it is a bit difficult with installation and it might be scary to see no GUI and only a command prompt for a beginner. = Getting Matlab = Please visit W&M IT [software support page http://www.wm.edu/offices/it/a-z/software/] and download Matlab from appropriate "Licensed Software >> Math & Statistics Software" section. They have several available versions. Either one is fine. Since we are learning Matlab, we will not have time to go to fancy toolboxes which Matlab provides/removes with new releases. = How to make a simple plot = Suppose during the measurements we obtain the following data for each current value 'I' we measured voltage 'V' with some uncertainty in voltage measurement dV. Our experimental data is represented in the following table. || I | V | dV | | 0.1 | 1.0 | 0.2 | | 1.0 | 2.2 | 0.1 | | 1.8 | 3.2 | 0.2 | | 3.3 | 3.8 | 0.2 | | 4.0 | 4.9 | 0.3 | First of all, we need to transfer it to the Matlab vector variables. Pay attention to the delimiter **;** between numbers ``` I = [ .1; 1.0; 1.8; 3.3; 4.0 ] V = [ 1.0; 2.2; 3.2; 3.8; 4.9 ] dV = [ 0.2; 0.1; 0.2; 0.2; 0.3 ] ``` Now we make a simple plot without error bars to get the basic, text after **%** is considered as a comment and Matlab will disregard it. ``` figure(1) % all plotting output will go to the particular window plot(I,V) ``` Easy, is not it? [simple_labeled_plot.png] But **every plot must be present with title, proper axes labels and units**. Note **'** for the string specifiers No our plot is nice and pretty. [simple_unlabeled_plot.png] = Fixing the range of the axes = Have a look at above plot. We see that one of the axes (y) does not contain origin point (y=0). Such plots are very common in finances and design to fool a reader, but they are generally not acceptable in scientific reports and definitely not in my class unless you have a good reason to do otherwise. The fix is very easy. Note that we are actually sending a vector [0,5] to the **``ylim``** function (fir x-axis we need to use **``xlim``** function). ``` ylim([0,5]) Now our plot is good to present [simple_labeled_and_proper_limits_plot.png] = Plotting with error bars = Let's open a new window for the plot ``` figure(2) Now we make plot with error bars assuming that they are the same for up and down parts ``` errorbar(I,V,dV) Yet again we need proper labeling ``` title('Dependence of voltage on current') xlabel('Voltage (V)') ylabel('Current (A)') ``` [errorbar_plot.png] = Fixing plots font size = Honestly Matlab is not the best tool to do presentation quality plots, it requires a quite a bit of messing with their GUI. But at least for a quick font size fix do the following. This might look like a magic spell so do read documentation on **``set``** command. ``` fontSize=24; set(gca,'FontSize',fontSize ); errorbar(I,V,dV) title('Dependence of voltage on current') xlabel('Voltage (V)') ylabel('Current (A)') ``` [errorbar_with_large_font_plot.png] = Saving you Matlab plots = Well it nice to have Matlab plots. But as soon as you close Matlab they will go away. How to save them for a future use? Actually, quite easy with **``print``** command. Unlike the name suggest it does not make a carbon copy but actually an electronic one. For figure saved in png format do ``` print('V_vs_I.png') or if you need a pdf (Matlab usually make very bad pdfs without a lot of tweaking) ``` print('V_vs_I.pdf') Make sure that you read documentation for **``print``** command there are some useful parameters. = Where to learn more = At W&M Physics 256 class teaches how scientists use computers. Matlab was a language of choice for this class. Feel free to read and use [my Physics 256 materials http://physics.wm.edu/~evmik/classes/2012_fall_practical_computing_for_scientists/] make sure that you read at least Lecture 02 slides. Also lecture 15 slides discuss data reduction and fitting.