summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--matlab_usage_source/fitted_data.pngbin0 -> 3575 bytes
-rw-r--r--matlab_usage_source/index.t2t126
2 files changed, 120 insertions, 6 deletions
diff --git a/matlab_usage_source/fitted_data.png b/matlab_usage_source/fitted_data.png
new file mode 100644
index 0000000..5b37fdc
--- /dev/null
+++ b/matlab_usage_source/fitted_data.png
Binary files differ
diff --git a/matlab_usage_source/index.t2t b/matlab_usage_source/index.t2t
index d09131e..4bc5b32 100644
--- a/matlab_usage_source/index.t2t
+++ b/matlab_usage_source/index.t2t
@@ -135,8 +135,8 @@ Yet again we need proper labeling
```
title('Dependence of voltage on current')
-xlabel('Voltage (V)')
-ylabel('Current (A)')
+xlabel('Current (A)')
+ylabel('Voltage (V)')
```
[errorbar_plot.png]
@@ -154,28 +154,142 @@ set(gca,'FontSize',fontSize );
errorbar(I,V,dV)
title('Dependence of voltage on current')
-xlabel('Voltage (V)')
-ylabel('Current (A)')
+xlabel('Current (A)')
+ylabel('Voltage (V)')
```
[errorbar_with_large_font_plot.png]
+= Fitting and data reduction =
+
+Modern experiments generate a lot of data, while humans can keep in their
+mind only about 7 things. So there is no way to intelligently work with
+large data set. We need to extract essential information out of our
+data i.e. do data reduction. Often we have some idea for analytical
+representation/model of the experimental data but we are not sure about
+the model parameters values. The process of extracting them is called
+**fitting**.
+
+So let's look at above Voltage vs Current dependence. According to the
+Ohm's law such dependence must be linear i.e **``V = R*I``**. Where
+**``R``** is constant coefficient called resistance.
+
+So first we need to define the model for Matlab. We **must** specify an
+independent variable name unless it is called **``x``**, which is not our
+choice here. Also, notice that **``fittype``** function wants to know
+independent variable //name// thus **``I``** is surrounded by **``' '``**
+
+```
+f=fittype( @(R,I) R*I, 'independent', 'I' )
+%______________^ independent variable must be last in the function arguments
+%____________^ even though R is supposed to be fixed it is still a variable
+% from fitting point of view
+```
+
+Now our job is simple, we need to specify a guessed value of the R as starting point
+for the fit function.
+
+``` param_guessed = [ 4 ]
+
+You might have a model with multiple parameters than it is a bit more
+evolved. Please, do read help file about fitting.
+
+Finally, we fit.
+```
+[fitobject, goodness_of_fit] = fit (I, V, f, 'StartPoint', param_guessed)
+```
+
+You will see the following
+```
+fitobject =
+
+ General model:
+ fitobject(I) = R*I
+ Coefficients (with 95% confidence bounds):
+ R = 1.291 (0.8872, 1.695)
+
+goodness_of_fit =
+
+ sse: 2.6340
+ rsquare: 0.7050
+ dfe: 4
+ adjrsquare: 0.7050
+ rmse: 0.8115
+```
+
+The most interesting part is **``R = 1.291 (0.8872, 1.695)``**,
+where 1.291 is the mean for the extracted parameter ``**R**``
+and **``(0.8872, 1.695)``** is the interval within one standard deviation.
+So we get our error bars as well:
+one sigma is equal ``1.695 - 1.291 = 0.4040``
+
+So our conclusion is that **``R = 1.3 +/- 0.4``**.
+
+== Checking the fitting result ==
+
+We should not trust computed fit parameters blindly. For our simple case it
+is probably OK, but for more evolved nonlinear fitting functions result
+might go way off if we specify starting point significantly far from
+optimal.
+
+One way to see check fit quality is to look at Root Mean Squared error
+(**``rmse``**) parameter from above output. It reflect what is the typical
+deviation of the model/fit from experimental points. It should be about the
+size of your experimental error bars. If it is much smaller then most
+likely you are over fitting (you model has to many free parameters). If it is
+much bigger your fit model is not good enough.
+
+
+Another way to check consistency of the fit it to look at plotted data and the fit
+simultaneously. Let's open yet one more window and plot our data first
+```
+figure(3)
+fontSize=24;
+set(gca,'FontSize',fontSize );
+
+errorbar(I,V,dV, 'x')
+
+hold on
+```
+
+notice **``hold on``** statement, now next plotting command will draw over presented plot i.e. graphics window //holds on// to already present content.
+
+Now we the plot corresponding to our model/fit. There are multiple way to do it,
+the simplest is
+```
+plot( fitobject )
+
+title('Dependence of voltage on current')
+xlabel('Current (A)')
+ylabel('Voltage (V)')
+```
+
+[fitted_data.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')
+``` print('-dpng', 'V_vs_I.png')
+
+If you find that you figures are to large specify resolution in dots per inch
+with **``-r``** option. For example
+``` print('-dpng', '-r50', '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')
+``` print('-dpdf', 'V_vs_I.pdf')
+
+The **``-d``** stands for output driver option, i.e png or pdf in above examples.
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