Scatter Plot

Correlation Analysis, Polynomial Fit, and Scatter Plot in Matlab

Here,the expert is trying to demonstrate to students the creation of a Matlab file for Correlation Analysis, Polynomial Fit, and Scatter Plot in Matlab. In this example loading of the data set is being done directly from excel. Here, variables are named in Matlab and names have been used in program. Correlation analysis has been done and the result is exported to excel.

SOLUTION : –

%% initialization

clc; clear; close all;

%% b

% reading excel files

m = xlsread(‘data_statistical_analysis_triamicinkids.xlsx’,’data’, ‘B2:F105’);

% setting variables

date = m(:,1);

brand_number = m(:,2);

DOLLLAR_SALES = m(:,3);

UNIT_SALES = m(:,4);

AVG_UNIT_PRICE = m(:,5);

%% c

figure; hold on;

plot(UNIT_SALES(1:52));

plot(UNIT_SALES(53:end));

xlabel(‘weeks’); ylabel(‘UNIT SALES’);

legend(‘Year 1’, ‘Year 2’);

% descriptive statistics

xlswrite(‘data_statistical_analysis_triamicinkids.xlsx’, min(UNIT_SALES(1:52)), ‘desc_stats’, ‘B3’);

xlswrite(‘data_statistical_analysis_triamicinkids.xlsx’, max(UNIT_SALES(1:52)), ‘desc_stats’, ‘B4’);

xlswrite(‘data_statistical_analysis_triamicinkids.xlsx’, mean(UNIT_SALES(1:52)), ‘desc_stats’, ‘B5’);

xlswrite(‘data_statistical_analysis_triamicinkids.xlsx’, var(UNIT_SALES(1:52)), ‘desc_stats’, ‘B6’);

xlswrite(‘data_statistical_analysis_triamicinkids.xlsx’, min(UNIT_SALES(53:end)), ‘desc_stats’, ‘C3’);

xlswrite(‘data_statistical_analysis_triamicinkids.xlsx’, max(UNIT_SALES(53:end)), ‘desc_stats’, ‘C4’);

xlswrite(‘data_statistical_analysis_triamicinkids.xlsx’, mean(UNIT_SALES(53:end)), ‘desc_stats’, ‘C5’);

xlswrite(‘data_statistical_analysis_triamicinkids.xlsx’, var(UNIT_SALES(53:end)), ‘desc_stats’, ‘C6’);

%% d

% correlation analysis of dollar sales

tmp = corrcoef(DOLLLAR_SALES(1:52), DOLLLAR_SALES(53:end));

xlswrite(‘data_statistical_analysis_triamicinkids.xlsx’, tmp, ‘year_corr’, ‘A2:B3’);

tmp = corrcoef(UNIT_SALES(1:52), UNIT_SALES(53:end));

xlswrite(‘data_statistical_analysis_triamicinkids.xlsx’, tmp, ‘year_corr’, ‘D2:E3’);

% yes they are strongly related …

%% e

figure; hold on;

plot(DOLLLAR_SALES(1:52));

plot(DOLLLAR_SALES(53:end));

xlabel(‘weeks’); ylabel(‘DOLLLAR SALES’);

legend(‘Year 1’, ‘Year 2’);

% dollar sales is decreasing in first half of year and then increasing in

% other half

saveas(gcf, ‘dollar sales’, ‘eps’);

%% f

figure;

p = polyfit(date(1:52), UNIT_SALES(1:52), 3);

subplot(121);

plot(date(1:52), polyval(p, date(1:52))); xlabel(‘date’); ylabel(‘UNIT SALES’); title(‘Year 1’);

p = polyfit(date(53:end), UNIT_SALES(53:end), 3);

subplot(122);

plot(date(53:end), polyval(p, date(53:end))); xlabel(‘date’); ylabel(‘UNIT SALES’); title(‘Year 2’);

%% g

figure;

p = polyfit(date, UNIT_SALES, 3);

subplot(121);

plot(date, polyval(p, date)); xlabel(‘date’); ylabel(‘UNIT SALES’); title(‘Degree 3’);

p = polyfit(date, UNIT_SALES, 6);

subplot(122);

plot(date, polyval(p, date)); xlabel(‘date’); ylabel(‘UNIT SALES’); title(‘Degree 6’);

%% h

figure;

subplot(121);

plot(UNIT_SALES(1:52), DOLLLAR_SALES(1:52), ‘*’);xlabel(‘UNIT SALES’); ylabel(‘DOLLLAR SALES’);  title(‘Year 1’);

subplot(122);

plot(UNIT_SALES(53:end), DOLLLAR_SALES(53:end), ‘*’);xlabel(‘UNIT SALES’); ylabel(‘DOLLLAR SALES’);  title(‘Year 2’);

% yes, there is a linear relationship

%% i

figure;

% year 1

x = UNIT_SALES(1:52);

y = DOLLLAR_SALES(1:52);

[x, index] = unique(x);

y = y(index);

p = polyfit(x, y, 1);

subplot(221);

plot(x, polyval(p, x));xlabel(‘UNIT SALES’); ylabel(‘DOLLLAR SALES’); title(‘Degree 1, Year 1’);

p = polyfit(x, y, 2);

subplot(222);

plot(x, polyval(p, x));xlabel(‘UNIT SALES’); ylabel(‘DOLLLAR SALES’); title(‘Degree 2, Year 1’);

% year 2

x = UNIT_SALES(53:end);

y = DOLLLAR_SALES(53:end);

[x, index] = unique(x);

y = y(index);

p = polyfit(x, y, 1);

subplot(223);

plot(x, polyval(p, x));xlabel(‘UNIT SALES’); ylabel(‘DOLLLAR SALES’); title(‘Degree 1, Year 2’);

p = polyfit(x, y, 2);

subplot(224);

plot(x, polyval(p, x));xlabel(‘UNIT SALES’); ylabel(‘DOLLLAR SALES’); title(‘Degree 2, Year 2’);