MATLAB Community

MATLAB, community & more

MATLAB Drive and British Coal

The other day I received this email from my friend Jos.
Very thoughtful of him! After a recent meeting, we were discussing the UK's notable decline in coal consumption. Jos had found some data from a government website and shared it with me via MATLAB Drive. Please note that all the numbers shown here have to do with the energy used to generate electricity (as opposed to, say, heat or transportation).
Because we both use MATLAB Drive, Jos can leave his code and data in place and just issue the invitation. Once I accept it, I can work in the same exact folder, thereby avoiding a lot of back and forth messaging with attachments.
I accepted the invitation and saw a folder with a spreadsheet, two import routines, and a file called runme.m.
Here's that runme.m file from Jos.
And when I run it, this is the result.
Sure enough, the usage of coal is now close to zero! But since Jos invited me to collaborate with this shared data, I thought I would fill out the analysis a bit more. Let's dig a little deeper.
Step one was to move his code into a Live Script (much later, I would take this Live Script and publish it directly to WordPress, so you're effectively looking at the Live Script I created).
Next, I loaded the data and cleaned up the table a bit more.
filename = "UK-energy-fuels.xls";
data = importdata(filename);
headers = importheaders(filename);
% Clean up some headers
headers(5) = "Hydro";
headers(7) = "Wind_Onshore";
headers(8) = "Wind_Offshore";
headers(end) = "Total";
% Missing values in data (NaN should be zero)
data(isnan(data)) = 0;
t = array2table(data','VariableNames',headers);
% Throw out some unused columns
t(:,[9 12 13 14]) = [];
Let's switch to a timetable. Timetables are awesome.
t.Time = datetime(1998:2019,1,1)';
t = t(:,[end 1:(end-1)]);
t = table2timetable(t)
t = 22×11 timetable
Time Coal Oil Gas Nuclear Hydro Wind Wind_Onshore Wind_Offshore Solar Bioenergy Total
1 01-Jan-1998 117.0348 6.8340 116.2895 90.5900 5.0939 0.8769 0 0 0 2.9637 355.1683
2 01-Jan-1999 101.2574 6.0717 139.6720 87.6721 5.3034 0.8510 0 0 0 3.7010 361.9159
3 01-Jan-2000 114.7365 5.9284 144.8912 78.3339 5.0586 0.9470 0 0 0 4.0673 371.4396
4 01-Jan-2001 125.4019 4.7766 138.7174 82.9850 4.0324 0.9650 0 0 0 4.7554 374.5751
5 01-Jan-2002 118.4754 4.2168 148.8707 81.0903 4.7628 1.2590 0 0 0 5.3066 375.0735
6 01-Jan-2003 131.7604 4.1707 145.1360 81.9114 3.2117 1.2880 0 0 0 6.2900 378.6893
7 01-Jan-2004 125.6896 4.0933 153.7336 73.6816 4.8205 1.9390 0 0 0 7.4275 380.8878
8 01-Jan-2005 128.5131 4.6509 149.2124 75.1728 4.7505 2.9120 0 0 0 8.9544 385.0995
9 01-Jan-2006 141.4950 5.4071 137.7543 69.2372 4.5655 4.2360 0 0 0 9.1093 381.3778
10 01-Jan-2007 129.0301 4.4649 162.3896 57.2489 5.0317 5.2879 0 0 0 8.5343 379.2790
11 01-Jan-2008 118.0531 5.8852 172.9876 47.6731 5.1109 7.1385 0 0 0 8.6219 378.2265
12 01-Jan-2009 97.7979 5.3651 163.4552 62.7617 5.1968 9.3016 0 0 0 9.6011 358.2008
13 01-Jan-2010 102.1764 4.3082 172.4532 56.4417 3.5658 10.3278 0 0 0 11.1318 364.3963
14 01-Jan-2011 103.0063 2.8074 143.8056 62.6552 5.6546 16.2075 0 0 0 11.8370 353.9106
ÿ
A quick stacked plot is a fun way to get our bearings. The nice thing about stacked plots is that they know all about timetables, so there's not much you have to worry about. Just one line of code, and no messing around with labels and limits.
stackedplot(t(:,[1 2 3 4 5 6 9 10]))
Since the y-scales are all different, the stacked plot is mostly useful for trends. Notice that Wind and Solar are moving steadily upward, while Hydro hasn't varied much. And there's Coal doing its disappearing act.
To understand the relative contributions of these energy sources, let's do an area plot.
Tables make this so pleasant!
h = area(t.Time,[t.Coal t.Oil t.Gas t.Nuclear t.Hydro t.Wind t.Solar t.Bioenergy]);
Fun fact: you can now set colors in MATLAB plots with CSS-style hex color designations. Let's find some appropriate colors for the chart. I picked these colors with this tool: https://www.w3schools.com/colors/colors_picker.asp.
sources = [ ...
"#4d4d4d", "Coal";
"#663300", "Oil";
"#999966", "Gas";
"#aa80ff", "Nuclear";
"#0066ff", "Hydro";
"#66ccff", "Wind";
"#ffff66", "Solar";
"#33cc33", "Bioenergy"];
for i = 1:length(h)
h(i).FaceColor = sources(i,1);
end
legend(sources(:,2), ...
"Location","northeastoutside")
xlabel("Date")
ylabel("Energy (TWh)")
grid on
Here's Jos's Coal plot, but normalized against the total, so we're looking at energy share as a percentage.
plot(t.Time, 100*t.Coal./t.Total, "o-","LineWidth",3)
grid on
xlabel("Date")
ylabel("Percentage")
title("Percentage of energy derived from Coal in the UK")
Who, at the beginning of this century, would have predicted this outcome? There is a certain poetic symmetry to the fact that Britain was the first country to make massive use of coal for industrial power, and it may well be the first to let it go.
There's an interesting side story here about the horse race between onshore and offshore wind. The UK has been a pioneer in offshore wind. You might say they're the Saudi Arabia of blustery sealanes.
plot(t.Time,[t.Wind_Onshore t.Wind_Offshore],"o-","LineWidth",3)
xlim(datetime([2012 2020],1,1))
legend({'Onshore','Offshore'},'Location',"best")
xlabel("Date")
ylabel("Energy (TWh)")
grid on
From a standing start at the beginning of the century, wind now generates 20% of the UK's power. And between the two flavors, onshore and offshore, as of 2019 it's a dead heat! Given the trends, you'd have to put your money on offshore wind. There's a lot more ocean than island. And fewer crinkly bits to get in the way.
As I was doing these plots, I couldn't help but notice that the total amount of energy consumed in Britain is shrinking over time. Which is all to the good. After all, it's much easier to use less coal when you need less overall energy in the first place.
plot(t.Time, t.Total,"LineWidth",3)
grid on
xlabel("Date")
ylabel("Energy (TWh)")
title("Overall power consumption in the UK")
Since I am a speculative person, and since MATLAB is the master of curve fitting, I naturally wondered: suppose this downward trend keeps up? What then?
tm = year(t.Time(8:end));
dt = t.Total(8:end);
% p is the polynomial equation for the linear fit to the data
p = polyfit(tm,dt,1);
% ti is where we intersect the x axis
ti = -p(2)/p(1);
t0 = 2005;
tf = [t0 ti];
df = polyval(p,tf);
hold on
plot(datetime(round(tf),1,1),df,"o-","Color","red")
hold off
xlim(datetime([2000 2100],1,1))
xlabel("Date")
ylabel("Energy (TWh)")
Based on current trends, we can confidently assert that the UK will need no power of any kind by 2096.
So thanks for this data, Jos! Sharing data and code on MATLAB Drive made this investigation easy. And now that I've done my bit, I'll see what Jos has to add. I'm curious to discover, in 80 years time, how he'll manage with no electricity.
|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.