How to write a curve-fitting Matlab script

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to my video tutorial today I'll be guiding you through the writing of a MATLAB script that can fit a curve to a set of data it can allow you to correlate that data to fiscal models and extract useful information I've generated this fake data set with some error not unlike something to lose T in a laboratory setting we'd like to plot this data instead of just staring at a list of numbers but before that you need to perform a couple of preliminary steps first of all we like to tell MATLAB what our working directory or folder is we do that by clicking the browse for folder button here and selecting the appropriate folder I've done this beforehand and you can see that our current folder is the one containing our data we import this data by clicking import data under the Home tab and opening our file we can now rename these variables I'd like to call them X and y and it's recommended that you import them one at a time as such note that our variables x and y are now under our workspace window this is just a fancy way of saying a collection variables we can save this workspace for later use by typing the command save open parenthesis single quote and in her file name I'll just call it dot mat closer single quotes and your parentheses and after he didn't entered you'll see that data mat is saved in the current folder start a new script by hitting ctrl on your keyboard or by hitting new script under the Home tab it is common practice to type commands CLC clear and close all in order to clear the command window fear of the workspace and close any open figures this is done so that MATLAB has a clean slate to work with every time you run the script however since we did use that clear command I'm going to have to load our workspace into the script we do that by typing load open parentheses a single quote and then our workspace file name data duck back open a new figure say taken one we can now plot X versus Y whoever I like to add an extra argument our plus the tells MATLAB that I want my data points to be plotted as red pluses run your script by hitting f5 on your keyboard or by hitting run under the editor tab since this is your first time rendering a script you're going to have to save the file call it something like cerf a.m. we can see that our data looks like a sine function which has a following general form notice that our wave height c1 appears to be about 5 c to our period of the time it takes to complete one full cycle appears to be about 10 and our horizontal and vertical offsets appear to be about 5 and negative 1 to find a new function D s as a difference squared between F and y the idea is that as these constants become closer and closer to the ones that best fit this curve the S of X will get smaller and smaller and eventually reach a minimum luckily MATLAB has a function f min search that could use these approximations to calculate the exact values that minimize D s I skipped ahead and wrote the MATLAB function in D s however I've included in the video description a download for every file I've used that way you could read through these comments that are left if you want to have a better understanding of what's going on behind the scenes all that's left to do is to minimize D s using F min search and plot of results we can do that by typing C equals F min search at DS and then our initial approximations which was five ten five and minus one to plot our curve on the same figure as our data set we must type hold on you can do this by typing F plot at our variable X and from here we can simply copy and paste from ds1 additional thing is required and that is a range of this function we can see from our figure that our function ranges from 0 to 30 running once again we can now see this blue curve that very nicely fits our data set was the exception of a few points but this is due to that error I generated and there you have it the exact constants are shown here below if you run into any problem or have any questions feel free to leave a comment below thank you for watching and happy coding
Info
Channel: Roberto Espinoza Jr.
Views: 43,656
Rating: 4.743772 out of 5
Keywords:
Id: Lw5GE7Xh6Og
Channel Id: undefined
Length: 4min 40sec (280 seconds)
Published: Mon Nov 07 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.