Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

provide a matlab code for this :Regression and Interpolation In this exercise, we are going to use MATLAB's built - in functions for regression and

provide a matlab code for this :Regression and Interpolation
In this exercise, we are going to use MATLAB's built-in functions for regression and interpolation. The goal is to determine whether regression or interpolation is the better choice for a given set of data. We are going to revisit two sets of data that you previously used.
clear; clc;
Upload the data to the MATLAB workspace. Assign the time column to the variable and the current column tot he variable . Plot this data using black circles as markers. Add figure titles and axes labels.
A = importdata('current.txt')
tc = A.data(:,1);
ic = A.data(:,2);
figure;
plot(tc, ic,'ko');
title("CURRENT VS TIME", "fontsize", 15)
xlabel("TIME (s)","fontsize", 15)
ylabel("CURRENT (A)", "fontsize", 15)
p = polyfit(tc, ic,1)
I_interpolated = interp1(tc, ic, tc)
grid on
Upload the data to the MATLAB workspace. Assign the time column from 1.5 seconds to 5.0 seconds to the variable and the corresponding current column to the variable . This time range corresponds to the index to . Plot this data using black circles and solid lines as markers. Add figure titles and axes labels.
B=importdata("normal.txt")
timerange =(tc>=1.5) & (tc<=5.0);
selectedtimes = tc(timerange)
tn = tc(timerange)
fn = ic(timerange)
plot(tc, ic,'ok', 'MarkerFaceColor', 'black', 'MarkerSize', 8);
title("CURRENT VS TIME", "fontsize", 15)
xlabel("TIME (s)","fontsize", 15)
ylabel("CURRENT (A)", "fontsize", 15)
grid on
TASK 1. Peform Regression on Data Sets
For both data sets, perform polynomial regression. To do this, use the function. The first two arguments are the x- and y-data set. The third argument is the order of the polynomial. Thus, if we're talking about linear regression, then the third argument should be 1. For now, we let the order be equal to 1. Assign the order to the variable in the field below.
current_data = importdata('current.txt');
x_current = current_data.data(:,1);
y_current = current_data.data(:,2);
% Perform linear regression on the current dataset
order_current =1;
We are now going to record the process time for the regression algorithm. This command begins the time recording.
tic % DO NOT CHANGE THIS COMMAND LINE.
The output of the function will be the coefficients of the polynomial equation, the first being the slope of the best fit line, while the second is the y-intercept. The polynomial function is given by
where is the fitting function and are the coefficients of generated from the function.
Use the function to determine the coefficients of the and data. Assign the output to the variables and , respectively. Remember to use the variable as the third argument. DO NOT PUT SEMICOLONS so that you can see the output values.
coefficients_current = polyfit(x_current, y_current, order_current);
This command stops the time recording.
toc % DO NOT CHANGE THIS COMMAND LINE. Record the elapse time in the table below.
Using the x-data set for current, , generate a new set of current values from the coefficients obtained from the . Assign these new data set to the variable .
y_fit_current = polyval(coefficients_current, x_current);
Plot the best fit line, and compare it in the same plot with the original current data, . Use the same black circles for the current data and a red line, with a LineWidth of 3 units, for the best fit line.
plot(x_current, y_current, 'ok', 'MarkerFaceColor', 'black');
hold on;
plot(x_current, y_fit_current, 'r-', 'LineWidth', 3);
hold off;
title('CURRENT VS TIME', 'fontsize', 15);
xlabel('TIME (s)', 'fontsize', 15);
ylabel('CURRENT (A)', 'fontsize', 15);
legend('Original Data', 'Best Fit Line');
n_current = numel(x_current);
error_current = sum((y_current - y_fit_current).^2)/ n_current;
% Display results
disp('Results for Current Dataset:');
disp(['Coefficients: ', num2str(coefficients_current)]);
disp(['Elapsed Time: ', num2str(elapsed_time_current),' seconds']);
disp(['Error: ', num2str(error_current)]);
Using the x-data set for current, , generate a new set of current values from the coefficients obtained from the . Assign these new data set to the variable .
normal_data = importdata('normal.txt');
timerange =(tc>=1.5) & (tc<=5.0);
selectedtimes = tc(timerange)
tn = tc(timerange)
fn = ic(timerange)
order_normal =1;
Plot the best fit line, and compare it in the same plot with the original current data, . Use the same black circles and solid lines for the current data and a red line, with a LineWidth of 3 units, for the best fit line.
coefficients_normal = polyfit(x_normal, y_normal, order_normal);
The equation for the error is shown in one of the video lectures and is given by
where is the number of data points and is the highest power or degree of the polynomial fitting function, .
Solve for the error corresponding to each fit. Use the function to make it easier for you to sum matrix elements. Assign the variables and to th

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

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

More Books

Students also viewed these Databases questions