Data Fitting in MATLAB: A COVID-19 Dataset

August 21, 2024
Kadeen Nayif Sarraf
UAE
MATLAB
Kadeen Nayif Sarraf has over 10 years of experience in MATLAB and holds a master's degree from the University of Sharjah in the UAE. He specializes in data analysis and fitting techniques.

MATLAB assignments often require data analysis and fitting, particularly when dealing with real-world datasets. In this blog, we will explore how you can effectively solve your curve fitting assignment that involve fitting data, using an example COVID-19 dataset to illustrate the steps and techniques. While this example focuses on a specific dataset, the principles and methods discussed can be applied to various data fitting tasks.

Understanding the Assignment Requirements

Data Structure and Loading

The first step in any MATLAB assignment is to understand the data structure and load it correctly. For instance, if you are given a .mat file containing structures with various fields, it is crucial to know how to handle such data.

Load the Data: Use the load function to import the data into your workspace.

 load('COVID_data.mat'); 

Inspect the Data: Examine the loaded data to understand its structure. Use commands like whos to check the variables and fieldnames to explore the structure fields.

 data = load('COVID_data.mat'); disp(fieldnames(data)); 

Setting Up Your Environment

To ensure a clean workspace and avoid conflicts with previous sessions, start your script with the following commands:

 clear all; close all; 

This command clears the workspace and closes all figure windows.

Steps to Tackle the Assignment

Loading and Inspecting Data

After loading the data, it is essential to inspect it to understand its structure and the information it contains. This step is crucial for extracting the relevant fields for further analysis.

1. Load and Inspect:

 data = load('COVID_data.mat'); dates = data.COVID_data.dates; infected = data.COVID_data.currently_infected; new_cases = data.COVID_data.daily_new_cases; new_deaths = data.COVID_data.daily_new_deaths; 

2. Explore the Data: Use commands like plot and hist to visualize the data and gain insights into its distribution and trends.

Data Preparation

Extract the relevant fields from the structure for analysis. This step involves selecting the data points that will be used for fitting.

1. Extract Relevant Fields:

 dates = data.COVID_data.dates; infected = data.COVID_data.currently_infected; new_cases = data.COVID_data.daily_new_cases; new_deaths = data.COVID_data.daily_new_deaths; 

Fitting Data

Data fitting involves using various functions to model the data and find the best fit. MATLAB offers several functions for this purpose, including polyfit, fit, and lsqcurvefit.

Polynomial Fitting

Polynomial fitting is a common technique for modeling data. The polyfit function fits a polynomial to the data, and polyval evaluates the polynomial at specified points.

1. Fit the Data:

 p = polyfit(dates, infected, 3); yfit = polyval(p, dates); subplot(3,1,1); plot(dates, infected, 'o', dates, yfit, '-'); title('Polynomial Fit of Currently Infected Cases'); 

Separate Peaks

If the assignment requires fitting data separately for different time periods, you can divide the data and fit each segment individually.

1. Fit Separate Peaks:

 before_date = dates <= datenum('25-May-2020'); after_date = dates > datenum('25-May-2020'); p1 = polyfit(dates(before_date), infected(before_date), 3); p2 = polyfit(dates(after_date), infected(after_date), 3); 

Advanced Fitting Techniques

For more complex assignments, advanced fitting techniques using functions like fit and lsqcurvefit can be employed.

Using fit Function

The fit function offers more flexibility in fitting types, allowing you to choose from various fit types.

1. Advanced Fit:

 f1 = fit(dates(before_date), infected(before_date), 'poly3'); f2 = fit(dates(after_date), infected(after_date), 'poly3'); subplot(3,1,2); plot(f1, dates(before_date), infected(before_date)); hold on; plot(f2, dates(after_date), infected(after_date)); title('Advanced Fit of Currently Infected Cases'); hold off; 

Using lsqcurvefit Function

The lsqcurvefit function allows fitting custom equations to the data, providing greater flexibility and precision.

1. Custom Fit:

 model = @(b,x) b(1) * exp(b(2) * x); b0 = [1, 0.1]; [b, resnorm] = lsqcurvefit(model, b0, dates, infected); yfit = model(b, dates); subplot(3,1,3); plot(dates, infected, 'o', dates, yfit, '-'); title('Custom Fit Using lsqcurvefit'); 

Best Practices

Maintaining clean, well-commented code and following best practices is essential for success in MATLAB assignments.

Code Commenting

Add meaningful comments to explain your code. Aim for at least 15-33% of your lines to be commented.

1. Example Commenting:

 % Fit a polynomial to the currently infected data p = polyfit(dates, infected, 3); % Polynomial coefficients yfit = polyval(p, dates); % Fitted values 

Code Formatting

Maintain proper indentation and minimize redundant code. Modularize your code using functions for clarity and reusability.

1. Example Modular Code:

 function yfit = polynomialFit(x, y, degree) p = polyfit(x, y, degree); yfit = polyval(p, x); end 

Validation and Testing

Test your code with different datasets to ensure robustness. Handle exceptions and edge cases gracefully.

1. Example Validation:

 % Test the function with a different dataset yfit_test = polynomialFit(test_dates, test_infected, 3); 

Turning in Your Work

To ensure your submission is correct and runs without errors, follow these guidelines:

1. File Naming: Name your main script file correctly (e.g., YourName.m).
2. Include All Necessary Files: Place all required files in a single ZIP or RAR file.
3. Test Your Code: Run your code multiple times to ensure it executes without errors.

Example Submission

1. Main Script File:

 clear all; close all; % Load and process data data = load('COVID_data.mat'); dates = data.COVID_data.dates; infected = data.COVID_data.currently_infected; % Fit and plot data yfit = polynomialFit(dates, infected, 3); plot(dates, infected, 'o', dates, yfit, '-'); title('Polynomial Fit of Currently Infected Cases'); 

1. ZIP or RAR File: Include all .m and .mlx files required to run your code.

Conclusion

By following the steps outlined in this blog, you can systematically approach and solve MATLAB assignments involving data fitting. Focus on understanding the data, using appropriate fitting techniques, and maintaining clean, well-commented code. These principles will not only help you with this assignment but also prepare you for similar tasks in the future.

MATLAB assignments can be challenging, but with the right approach and techniques, you can successfully analyze and fit data, providing valuable insights and solutions. Happy coding!

Comments
No comments yet be the first one to post a comment!
Post a comment