Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Creating and Using Custom Functions in Matlab - Prof. Wonhwa Cho, Exams of Biochemistry

How to write and use custom functions in matlab, using the example of creating a function that calculates the error (chi-squared) of a fit. The basics of function syntax, including the use of input arguments and the return value. It also demonstrates how to load data from a file and use it within a function, as well as how to use matlab's built-in functions like 'fminsearch' to find the best fit parameters. The document concludes by emphasizing the flexibility of custom functions in matlab for fitting data to any functional form.

Typology: Exams

2010/2011

Uploaded on 05/04/2011

svaruag81
svaruag81 🇺🇸

2 documents

1 / 5

Toggle sidebar

Related documents


Partial preview of the text

Download Creating and Using Custom Functions in Matlab - Prof. Wonhwa Cho and more Exams Biochemistry in PDF only on Docsity! Matlab Functions    In Matlab,  every mathematical  function  (such  as  sin)  is  actually  a  series  of  instructions  in  a  “function_name.m” file. In my example, when I type in the consul:    >>sin(3.14)   ans =      0.0016    What is happening is the number 3.14 is being sent to file sin.m for processing such the function  returns  the  value  0.0016.  (in  my  computer,  the  file  sin.m  is  located  at:   C:\ProgramFiles\MATLAB\R2006b\toolbox\eml\lib\matlab).  The  interesting  thing  is  you  can  write your own functions that can do whatever you want, like calculate the error in a fit to your  data.    Let’s write our first function.  It’s easier to  just go under File → New → M‐file. You should see  this:      On the first line type:  function [return_val]=fitter(x)   Now here is what these things do:    function → This tells Matlab that your writing a function. What this means is that the function is  “blind” to the consul (the thing you’re typing commands in). If dataset has been loaded into the  consul,  the  function  still  cannot use  it.  Likewise,  if dataset  is altered  in  the  function,  it  is not  altered in the consul‐ several examples of what this means are given below.    [return_val] → This is the value that the function will return, in our example above for sin(3.14),  it was 0.0016.    fitter → The name of the .m file. When you save it, make sure you save the name of the file as  “fitter”.    (x) → This  is what you pass to the function,  in our previous example of sin(3.14), x  is equal to  3.14 inside the function. It can also be a vector, in other words, it can have two values; x(1) and  x(2) for example.    Now  let’s write  a  function  that multiplies  two  numbers  together.  In  your  .m  file, write  the  following:    function [return_val]=fitter(x) kitty=x(1)*x(2); return_val=kitty;   Now save the .m file as fitter.m. In the consul, type:  >>x(1)=5; x(2)=6;  >>fitter(x)  ans =      30    See? It’s really easy.  Note the following: change your .m file as:  function [return_val]=fitter(x) i=444; kitty=x(1)*x(2); return_val=kitty;   Save it and type the following:  >>x(1)=5; x(2)=6; i=666;  >>i  i =     666  >> fitter(x)  ans =      30  >> i  i =     666    Note that the value of i appears to be redefined in fitter.m from the value 666 to 444; however,  it is actually unchanged from your assignment of 666 in the consul. This is what I mean when I  say that the consul is “blind” to the actions of the function and vise versa.     Now  let’s write something relevant‐ a  function  that can calculate  the error of a  fit. By error,  I  mean 2 (chi squared). From your laboratory manual, 2 is:                N i ifitidata i1 2 2 2 )()( )( 1    
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved