File Exchange

image thumbnail

fitVirusCV19v3 (COVID-19 SIR Model)

version 3.2.4 (2.96 MB) by Joshua McGee
Estimation of coronavirus COVID-19 epidemic evaluation by the SIR model, Code receives and plots most recent data from HDX.


Updated 03 Apr 2020

View License

This code will retrieve recent data from the HDX and attempt to fit it to a SIR model using structure created by Milan Batista ( The model is data-driven, so its forecast is as good as data are. Also, it is assumed that the model is a reasonable description of the one-stage epidemic.

Results are saved in structure res (see function fiVirusCV19 header). Optionally the results may be printed by:


The plot may be enabled or disabled via (default is on):

A more detailed description can be found in:
Examples can be found in:

Cite As

Joshua McGee (2020). fitVirusCV19v3 (COVID-19 SIR Model) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (46)

Hi, Mr. Joshua! I succes run the it, but failed ( error ) to get some plot such as United Arab Emirates, Guam, New Caledonia.
On the otherhands the results for Turkey and Russia is good but the date axis for Turkey in case/day graph is unusual, the fit result of Russia show the large number of C_end.
Would you mind to explain these factors?
I am begging you.
Best regards!


I am sorry, I just saw a similar question put on 30 march without reply


Hi, Mr. Joshua! It's very impresive work you done here!
I am from Hongkong( born in Taiwan). If I may ask, could you make update for my country Hongkong and Taiwan, please?
Your work will help us.
And please give the update for the exmaples (maybe) every week? ( If you can't )
Thank you very much for your kindness!

Thank you very much, Mr. Joshua McGee for updating my country Japan and the others!
Sorry to asking you a basic question. How can I install this model source?P.S. I used to use Python for my analytic.
I try to use it to see the other countries such as Russia, Vietnam, Turkey.
If you can tell me or update it for me, it would be great.
Thank you very much again! GBU

dong gretch

please give link of latest version

Joshua McGee

Hi Riel,
Thanks for reporting this bug. It should be fixed now. Please download the latest version.

Riel Aljama

Good morning Joshua,
I was able to run the your code now.
but I am having problem with some country, particularly Philippines

Fail to obtain parameters.
ini: beta = 0.682172 gamma = 0.454782 N = 8016.47 I0 = 0.00515606
calc: beta = 0.0543995 gamma = -0.219728 N = 25201.8 I0 = 0.000941286
Unrecognized function or variable 'plt'.

Error in fitVirusCV19v3>plotData (line 781)
if plt

Error in fitVirusCV19v3 (line 294)

Joshua McGee


Hi Mr Joshua McGee!
Thank you very much for all of these.
Could you do me a favor to add example case in Japan, my country?
Also if it is not trouble you, could you add Singapore and Australia too ?
Thank you very much. GBU

Joshua McGee

Hi Clarke! Thanks for your kind words. I'll work on incorporating per capita testing into my next iteration of the model.

Hi Joshua, thank you very much for all this work. I'm wondering if you think increasing per capita COVID-19 testing rates here in the US (compared to other nations) will affect your total infected forecast?

Joshua McGee

Hi Marco, this code requires 2019b or greater.


Hi Joshua, this seems great. I am trying to run it but I get an error ... It seems like detectImportOptions is missing, so I am a bit stuck.
I am running on R2016a so it may be a bit dated.
Thanks for any tip

>> fitVirusCV19v3("Italy")
'lambertw' requires Symbolic Math Toolbox.

Error in fitVirusCV19v3>calcEndPoint (line 628)
lambertw(-beta*(1 - c0)*exp(-beta/gamma)/gamma);

Error in fitVirusCV19v3>calcClim (line 601)
res = calcEndPoint(beta,gamma,I0/N)*N;

Error in fitVirusCV19v3 (line 306)
Clim = calcClim(b);

Unfortunately, I don't have access to Symbolic Math Toolbox, and it's not listed as required to run the code. Any thoughts or suggestions?

Joshua McGee

added Indonesia to examples

Joshua McGee

Will do Anton.

Anton Yess

Hi Joshua,
Would you mind making on examples tab for Indonesia? Thx.

Error using fitVirusCV19v3 (line 152)
Table variable names must be character vectors.
I'm using 2018 version of MATLAB..

Joshua McGee

-updated, attempted again to fix compatibility issues with older MATLAB versions

Joshua McGee

Will do. I’ll update later today.

St Se

Could you update the 'examples' graphs every couple of days maybe? Several countries approach the tipping point soon. Thanks

Joshua McGee

I’ll look into this.

on R2019b there is the following error:

at = fitVirusCV19v3("Austria");
Error using odearguments (line 21)
When the first argument to ode45 is a function handle, the tspan argument must have at least two elements.

Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);

Error in fitVirusCV19v3 (line 327)
[t,Ca] = ode45(@(t,y) odeFun(t,y,b), tspan, I0);

Giovanni B.

Joshua McGee

It is fully functional for 2019b and up.

Joshua McGee

Yes this is a compatibility issue. I’m working on a workaround.

Hi! Cool model.
I keep geting:

Error using fitVirusCV19v3 (line 149)
'01_22_20' is not a valid variable name.

Error in run (line 1)
it = fitVirusCV19v3("Italy");

Any suggestions?

Joshua McGee

Hi Tosaporn, I’m working on a fix that will accommodate most MATLAB users. This is compatible with 2019b and up.

Hi Joshua,
I tried the code, it showed Error , Can you please explain?
at = fitVirusCV19v3("Austria");
Error using fitVirusCV19v3 (line 152)
'01_22_20' is not a valid table variable name.
See the documentation for isvarname or
matlab.lang.makeValidName for more information.

Joshua McGee

Hi Giuseppe,
The database was probably experiencing too much traffic at the time. Wait a little while and try again.

Leon Burger

Hi, these are my results on R2019a; any work around. Want to run scenario for South Africa, but using Italty as a test case.

Error using readContentFromWebService (line 46)
The server returned the status 403 with message "FORBIDDEN" in response to the request to URL

Error in webread (line 125)
[varargout{1:nargout}] = readContentFromWebService(connection, options);

Error in fitVirusCV19v3 (line 124)

Joshua McGee

Hi Riel,
Thanks for your patience. Please try again now. I converted the variable name dates to datenum format.

Riel Aljama

Hi, these are the result

Error using fitVirusCV19v3 (line 148)
'22-Jan-2020' is not a valid variable name.

Joshua McGee

Hi please try again now.

Riel Aljama

Good Day!
I keep on getting this error :( (using 2018a)

Error using fitVirusCV19v3 (line 148)
'Province/State' is not a valid variable name.

Joshua McGee

All compatibility issues should now be fixed

Joshua McGee

This is a compatibility issue. What version are you running?

any ideas on why I would get this error:

Unrecognized property 'PreserveVariableNames' for class

Error in fitVirusCV19v3 (line 128)
opts.PreserveVariableNames = true;

St Se

Very nice to see the graphs being updated.

St Se

I had the same problem. Matlab 2019.b with mex installed.

excellent work, but when I run the code it gives me this error:
Unrecognized property 'PreserveVariableNames' for class
Error in fitVirusCV19v3 (line 132)
opts.PreserveVariableNames = true;


excellent work, but when I run the code it gives me this error: Error using fitVirusCV19v3 (line 170)
Duplicate table variable name: 'NaN'.

any suggestion?



-improved initial parameter approximation (courtesy of Milan Batista)


-updated examples


-updated with data from 4/2




-updated 4/1


-bug fixes


-updated 3/31 8:40 PM EST


-updated aesthetics of examples


-updated examples


-updated plots for today (3/30)


-bug fix


-updated examples




-compatibility notes


-updated examples


-updated plots and fixed small bug


-added death rate to example


-updated examples


-added ability to disable plot and created a live script to tabulate the R0 values of each country


-added Indonesia


-bug fixes and compatibility




-attempted to fix additional variable name Compatability issues




-fixed all compatibility issues


-bug fixes






-updated date ticks


-new data source (updated database)




-updated compatibility


-updated description


-applied updates courtesy of Milan Batista (RMSE correction, automatic parameter selection)


-name change


-added tags


-fixed live script


-added live script to generate plots

MATLAB Release Compatibility
Created with R2019b
Compatible with R2019b to any release
Platform Compatibility
Windows macOS Linux

Inspired by: fitVirusCOVID19