Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please explain what is each part of this matlab code: % % part 2 % Step 1 : Import the two datasets and combine them

Please explain what is each part of this matlab code:
%%part 2% Step 1: Import the two datasets and combine them into one global datasetdata1= readtable('2021_Cars_Aggregated.csv'); % Import the first datasetdata2= readtable('2021.Vans_Aggregated.csv'); % Import the second dataset%Set a fixed seed for reproducibilityseed =50; % Choose any integer value for the seedrng(seed); % Set the random number generator seed% Sample data for Diesel and Petrol carscarPosition = linspace(1,60,50);% Assumed CO2 emissions for Diesel and PetrolCO2Diesel =20+2* exp(-carPosition /60*2* pi)- randn(1,50)*2;CO2Petrol =18+2*(carPosition /60*2* pi).^0.3+ randn(1,50)*2;% Fit polynomial curves with a reduced degree of 2pDiesel = polyfit(carPosition, CO2Diesel, 2);pPetrol = polyfit(carPosition, CO2Petrol, 2);% Generate points for best fit linesfitDiesel = polyval(pDiesel, carPosition(carPosition<50));fitPetrol = polyval(pPetrol, carPosition);% Plotting the datafigure;hold on;% Plot Diesel best fit lineplot(carPosition(carPosition<50), fitDiesel, 'Color', [1,0.5,0], 'LineWidth', 2);% Plot Petrol best fit lineplot(carPosition, fitPetrol, 'Color', [0.5,0,0.5], 'LineWidth', 1);% Petrol datascatter(carPosition, CO2Petrol, 'o', 'MarkerEdgeColor', [0,0.5,1]); % Blue for Petrol% Diesel datascatter(carPosition, CO2Diesel, 'o', 'MarkerEdgeColor', [1,0.5,0]); % Orange for Diesel% Customize the plotxlabel('Car Position');ylabel('CO2 Weighted Percentage');title('Weighted CO2 Emissions');xlim([160]);ylim([1535]);% Add a legend with custom nameslegend('Diesel Best Fit', 'Petrol Best Fit', 'Petrol', 'Diesel');% Add grid linesgrid on;hold off;% Combine the datasetscombined_data =[data1; data2];% Step 2a: Create a bar chart showing how many cars use distinct fuel types% Preprocess data: remove missing or empty values from 'FuelType' columnvalid_indices = ~ismissing(combined_data.FuelType) & ~strcmp(combined_data.FuelType, '');% Filter the combined data based on valid indicesvalid_fuel_data = combined_data(valid_indices, :);% Get unique fuel types and count occurrences[fuel_types, ~, fuel_type_indices]= unique(valid_fuel_data.FuelType);fuel_counts = histcounts(fuel_type_indices, 1:numel(fuel_types)+1);% Plot the bar chartfigure;bar(1:numel(fuel_types), fuel_counts);title('Number of Cars Using Distinct Fuel Types');xlabel('Fuel Type');ylabel('Number of Cars');xticks(1:numel(fuel_types));xticklabels(fuel_types);% Step 2b: Create a scatter plot showing the weighted CO2 Percentage for each car[sorted_manufacturers, manufacturer_indices]= sort(combined_data.Manufacturer); % Sort manufacturers alphabeticallysorted_CO2_percentage = combined_data.CO2_Percentage(manufacturer_indices); % Sort CO2 Percentage accordinglySet a fixed seed for reproducibilityseed =50; % Choose any integer value for the seedrng(seed); % Set the random number generator seed% Sample data for Diesel and Petrol carscarPosition = linspace(1,60,50);% Assumed CO2 emissions for Diesel and PetrolCO2Diesel =20+2* exp(-carPosition /60*2* pi)- randn(1,50)*2;CO2Petrol =18+2*(carPosition /60*2* pi).^0.3+ randn(1,50)*2;% Fit polynomial curves with a reduced degree of 2pDiesel = polyfit(carPosition, CO2Diesel, 2);pPetrol = polyfit(carPosition, CO2Petrol, 2);% Generate points for best fit linesfitDiesel = polyval(pDiesel, carPosition(carPosition<50));fitPetrol = polyval(pPetrol, carPosition);% Plotting the datafigure;hold on;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

Students also viewed these Databases questions