MatLab Least Squares fit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay let's start by firing up MATLAB it's in the screen hopefully you can see that and I'm just gonna go ahead and start a new script because the script will be a little bit easier if I make a mistake I'll just change it and keep going back don't get it right so first thing I wanna do is I want to put in my X values and my X values were 1 2 3 4 5 ok and then let's put in the Y values and they were 5 7 15 12 and 17 the reason I'm putting these in in a column I'm making them a column matrix a little space there so it's easy to say okay let's plot that oops always making mistakes ok let's go ahead and we'll run that we'll call this put this in I'll upload this on the website so you guys can get at it class 4 and we'll call this class B Square okay let's and good and run that oops so I see it's not really what I wanted I wanted to show the raw data so let's do that I didn't really want to find that and close it okay okay let's try again okay yeah that's more like it so I want to least squares fit this data that I have right here okay so let's continue on with our quest the other thing i want i want the axis I really would like it to go from zero to five and zero to 20 because otherwise it puts in these random values I don't like that so do I have that right yes I do okay so I'm going to say hold these the one with my least squares over top of that at some point and then a1 is the matrix that we we took them talked about okay so I'm just creating a 2x2 holder okay for that and then I'm going to create a four loop I equals one colon five or five data points it indexes it for me so I have a one of I comma one so that first column will equal my X values X okay semicolon and a one of I comma two the second one as we saw a cool equals 1.07 colon and that will pretty much wrap up that so now I just need to do my multiplication okay and I need to have inverse okay that was checked let me just say let me just go ahead op create a variable R which will be my a and B term inverse a1 transpose and then use for the dot product is that that's multiplication times and it just it you don't have to do this but I put in parents to me a little bit easier okay so that gives me the a and B value and then I'm gonna go ahead and create two endpoints T equals zero five so there's my two endpoints okay and then y1 will create this is gonna be my line that I'm kind of creating here will equal R of 1 times T plus R of to okay this is my a and B values okay and then I'm just gonna go ahead and plot T comma Y 1 comma and now I want a straight line I wanted to just fit it put a semicolon there let's just go ahead and do that I think I've got everything there was probably got a mistake in it we'll find out a second and voila no mistake okay so that looks pretty good so let's do a second water system just for fun we'll go ahead and do it make it a second water system okay so if I do a second order system I am going to let's see I need this to be okay and then this 2 will be X okay and then a one of I comma three will equal the 1.001 so we just checked that so it's 2 squared the next one so yeah ax squared plus BX plus C okay so the second water system that's gonna give me a perf it for this the difference here is mighty has to be on that's it one has to be a continuous system so we'll just make it continuous at point one intervals okay to make this work and then our one will be x t dot squared plus oops took the plus r2 times t plus r3 okay and does that look right I think that looks great I just squared okay that's got plus R there's no dot needed air r3 and then we'll just plot that if it's wrong it'll it'll blow up Oh nope there we go and there's a second water ok system ok that I can plot there so I mean I could do a third order too cuz I've got enough data so then this would be come to the third power this would become squared this would become X of I and then a i-4 would be one that would be let's just get into it let's go ahead and do it so what the heck okay so this would be three this would be squared this would be X of I and then I would have an a.1 of I comma four equals one point that's the last one okay yeah we're living living large right now so let's just go ahead and put our 1 this is going to be third power then our two times t dot squared are three times T plus r4 and provided to make any typos I think that's okay and there's the third order system so not too bad that's pretty good for tonight so I'm gonna add this to the other video and that's gonna be the last video I did tonight it's 12:30 I'm starting it late
Info
Channel: Stephen Wilkerson
Views: 35,507
Rating: 3.7210884 out of 5
Keywords: Least Squares Fit, Linear data Fitting
Id: amM9F3zwIrM
Channel Id: undefined
Length: 8min 19sec (499 seconds)
Published: Mon Sep 14 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.