Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please explain each step that happens in this matlab code. Please give small explantiom to each step. This code makes 5 graphs. % Step 1

Please explain each step that happens in this matlab code. Please give small explantiom to each step. This code makes 5 graphs.
% Step 1: First we need a date vector from January 1st,2024 to December 31st,2024start_date = datetime(2024,1,1);end_date = datetime(2024,12,31);date_vector = start_date:end_date;% Step 2: Trying to find associated days of the weekdays_of_week = day(date_vector, 'name');% Step 3: Here is how to make a random temperature vectortemperature_max =120;temperature_min =55;temperature_vector =(temperature_max - temperature_min)* rand(size(date_vector))+ temperature_min;% Step 4: Creating a string for the weekend daysweekend_days = string(date_vector(weekday(date_vector)==1| weekday(date_vector)==7));% Step 5: Development of more substance to the report%(a) Here is a linear plot for weekends in which the temperature is over 100weekend_indices = find(temperature_vector >100 & (weekday(date_vector)==1| weekday(date_vector)==7));figure;plot(date_vector(weekend_indices), temperature_vector(weekend_indices),'-o');title('Weekend Temperatures over 100');xlabel('Date');ylabel('Temperature');%(b) This section is a bar plot showing how many times the temperature is below 85 in each monthmonths = unique(month(date_vector));temperature_below_85_count = zeros(size(months));for i =1:numel(months) month_indices = find(month(date_vector)== months(i)); temperature_below_85_count(i)= sum(temperature_vector(month_indices)<85);endfigure;bar(months, temperature_below_85_count);title('Count of Days with Temperature Below 85 in Each Month');xlabel('Month');ylabel('Count');%(c) Here we have two linear plots showing the maximum and minimum temperature in each monthmax_temperature_monthly = accumarray(month(date_vector)', temperature_vector', [], @max);min_temperature_monthly = accumarray(month(date_vector)', temperature_vector', [], @min);figure;plot(months, max_temperature_monthly, '-o', 'DisplayName', 'Max Temperature');hold on;plot(months, min_temperature_monthly, '-o', 'DisplayName', 'Min Temperature');hold off;title('Monthly Maximum and Minimum Temperatures');xlabel('Month');ylabel('Temperature');legend('Location', 'best');%% Part 2: Imported Data% 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 with AI-Powered 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

Students also viewed these Databases questions