getting started with matlab video -凯发k8网页登录
get started with matlab® by walking through an example. this video shows you the basics, and it gives you an idea of what working in matlab is like.
the video walks through how to calculate solar panel energy production. you’ll see how to import data, define variables, and perform calculations using various elements of the matlab desktop environment, including the command window, the workspace browser, and the variables editor. visualize data using prebuilt plots, and then customize those visualizations. you’ll also learn how to use the documentation to find built-in functions, guidance on their syntax, and code examples that demonstrate how to use the functions.
lastly, you’ll see how to use the live editor to create scripts that combine code, output, and formatted text in an executable notebook that can be shared with others.
matlab® is an environment for all kinds of technical computing—like data analysis, simulation, and algorithm development. this video will show you the basics and give you an idea of what working in matlab looks like. be sure to stay to the end to find out where to go next to learn matlab in depth. so, let's get started.
this is one of the buildings at mathworks headquarters in natick, massachussetts. see all those nice solar panels? well, let's see if they're working properly. there's a theoretical model that says what the production should be. let's implement that and compare it with the actual data recorded from the panels.
first, we need some constants: the latitude of natick and the “solar declination,” which is just an angle that tells us how high in the sky the sun gets on a given day. these are values we can look up. let's use the value for june 21, the longest day of the year, so that will give us the maximum amount of production. our calculations are entered in the command window and executed immediately and we can see the variables we've just created over here in the workspace.
the angles we've just entered are in degrees, but if we're going to do math with them, it might be better to convert them to radians. we can do standard mathematical calculations and assign the result to a new variable or even overwrite the same variable. here we're using the built-in value of pi to do the conversion manually. but we could also use one of the many built-in matlab functions.
next, we want to calculate the production throughout the day, so we need a range of times. let's make a vector to represent time of day. we'll start at 5:30, a little after sunrise, and go in 15-minute increments until 8:00, just before sunset.
our formula uses local solar time. that's not exactly the same as the time on the clock, because of conventions like time zones and daylight savings. so, we'll take our vector of times and apply a shift.
and now we're ready to calculate the effect of the angle between the sun and the panels. this equation is long, but the matlab code looks just like the math, so it's easy enough to implement. again, this part of the formula is assuming degrees instead of radians, so we could convert or... we could look in the documentation to get more information about trigonometric functions, where we discover that there’s a cosd function that accepts inputs in degrees rather than radians. matlab has functions for all sorts of things, from trigonometry to outlier detection to curve fitting to graph theory to signal filtering. so it’s always good to check the documentation.
now that we know about cosd, we can complete our formula. and let's add a semicolon to the end of the line so the result isn't displayed. if we want to look at the values, we can always double-click on the sunangle variable in the workspace: it opens up the variable editor. but it's probably more informative to view it graphically. we can select the variables t and sunangle, and go to the plots tab in the toolstrip. select a plot and there it is. and now we also have the code so we know how to do that programmatically next time. the intensity of radiation due to the angle of the sun should be 0 as the sun rises and sets and peak at local noon. and we should get 100% intensity when the sun is directly over the panels. but in massachusetts the sun never gets directly overhead, even in june, so the plot looks about right.
okay, that’s the angle of the sun accounted for. to complete the model, we need to calculate the effect of the atmosphere. the more air the light has to get through, the less energy makes it to the panels. let's enter this empirical equation and.... oops, something went wrong. fortunately, this helpful error message lets us know that we made a common mistake. matlab works naturally with vectors and matrices, including doing matrix math. so, by default, matlab thinks this carat is a matrix exponential. but that's not what we meant—we want the exponent for each element of the array, so let's use the up arrow to recall that command, then do what the error message says and change the exponent operation to dot-carat.
finally, we just need to multiply the two intensity factors together (and we've learned from the previous error, so we'll use dot-star this time), and multiply by the size of the panels to get the total theoretical energy production. check the plot—looks reasonable, so there it is: the theoretical maximum production we should get from our solar panels. this is what we should see on june 21st, if it’s a perfect sunny day. next, we need to get the actual data and compare the two.
but before doing that, it might be a good idea to save what we've done in a script. let's go back through the command history and select the commands we used to get here, right-click, and select create live script. this opens the editor with a script containing the selected commands. we can now edit commands, and because we have a live script, we can make this more useable by splitting into sections, adding text, comments, headers, images, equations, and so on. now we can run sections of code, or the whole script, and the output appears in the output panel next to the code. we can use the interactive tools to clean up our plot. and, again, we get the code so we can add it to our script.
now for the data. in the current folder browser we can see we have a spreadsheet that contains the recorded production for june 2018. let's import that data. the import tool looks at the contents of the file. it recognizes the first column as timestamps, so wants to import those in a data type that's suitable for dates and times. it also wants to import all the data together as a table, which is a data type designed for this kind of spreadsheet data where we have a bunch of observations of several different variables. so, let’s just import the data in this form, but maybe with a slightly simpler variable name. now we have this variable, production, which is a table with 2,880 observations of three variables. the three variables are time and the electricity produced by two different solar panel arrays.
having imported some data, a good first step is often to plot it to get an idea of what you’re dealing with. so, let’s use the plot function. to get the individual variables within the table, we use dot notation – the name of the table, dot, and the variable name. and note the useful programming aids that suggest completions. run this section of the script to see the results. because the timestamps were imported as a datetime variable, the x axis of our plot is labeled as dates, so we can see the 30 daily spikes for the month of june. we can use the interactive tools to explore the plot a bit. we can see there were some cloudy days, including the 21st, unfortunately. but over here you can see the 26th was perfect.
so how do we get the production for one chosen day? well, there are several different ways we could do this, but if we’re interested in slicing the data by day or time of day, it might be useful to rearrange our data from one continuous time series to a grid of times and days. this approach makes sense for this data which is recorded uniformly every 15 minutes, so the 2880 measurements for june correspond to 96 measurements – 4 per hour – for each of the 30 days in the month. so, let’s use the reshape function to change the long vector into a 96-by-30 matrix.
now it’s easy to extract the data for any given day. to get the data for the 21st, we go into our matrix and take all rows of the 21st column. this data is recorded throughout the day, so we need to make a vector of times from midnight to midnight and now we can plot it. let’s add a style specification to show the actual data points.
and now we can give both the theory and the data to the plot function so we can see them together. as expected, the data for the 21st isn’t very good. but remember that the 26th did look good and a few days won’t change the angle of the sun that much, so let’s look at that day. thankfully, it’s easy to change to a different day and rerun the section.
now we can see that the data agrees with the model, well, up to a threshold of how much the inverters can handle. for our system, the panels can produce up to 270 kw, but the inverters have a limit of 207 kw. we can go back and use the min function to add this limiting behavior to our model. rerun the script... and now we see that the data agrees very well with the theoretical model.
we’ve done some great work here. so, we should share it. if we just want to share our findings with someone, we could save a copy of the script as a static document, like a pdf. but we can also give this script (along with the data file) to anyone with matlab and they can run it for themselves and reproduce our results. they can edit the script, explore the data, refine the model, and perform new analysis.
and you can, too. these files are available for you to download.
and, now that you have a feel for what working in matlab is like, it’s time to learn it properly. there’s no better way to learn matlab than to work with it. so, head over to matlab onramp, which will teach you the basics of matlab interactively – you’ll actually enter matlab commands in our online training environment and get instant feedback. it’s free and should take just a couple of hours. you can leave any time and come back later. welcome to matlab!
download code and files
related products
learn more
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 mathworks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- (español)
- (english)
- (english)
欧洲
- (english)
- (english)
- (deutsch)
- (español)
- (english)
- (français)
- (english)
- (italiano)
- (english)
- (english)
- (english)
- (deutsch)
- (english)
- (english)
- switzerland
- (english)
亚太
- (english)
- (english)
- (english)
- 中国
- (日本語)
- (한국어)