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 that MathWorks headquarters in Natick, Massachusetts
See all those nice solar panels. Well, let's see if they're working properly
There's a theoretical model that says what the energy production should be
Let's implement that and compare it with the actual data recorder 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 that we can look up
Let's use the value for June 21st 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. 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 gonna 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 o'clock 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 saving
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 coast d function that accepts inputs and degrees rather than radians
MATLAB has functions for all sorts of things. So it's always good to check the documentation
Now that we know about Coastie we can complete our formula and what'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 Sun angle 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 Sun angle and go to the plots tab in the tool strip
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 zero as the Sun rises and sets and
peak at local noon and
We should get a hundred percent intensity when the Sun is directly over the panel's 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 pass through the less energy makes it to the panels
Let's enter this empirical equation and oh
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 carrot is a matrix exponential, but that's not what we met
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 caret
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 use 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 the commands and because we have a live script we can make this more usable 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 plots. 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 that 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 the data type that's suitable for dates and times
It also wants to import all the data together as at a which is a datatype designed for this kind of spreadsheet data
Where we have a bunch of observations and 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
2880 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 a 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 time stamps were imported as a date/time 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 are some cloudy days including the twenty-first unfortunately, but over here you can see the 26 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 for 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 x 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 kilowatts, but the inverters have a limit of 207 kilowatts
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 can 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 on-ramp, 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 anytime and come back later
Welcome to MATLAB