In this problem, you have to predict the marks of one subject (Physics) using best fit…
- User
In this problem, you have to predict the marks of one subject (Physics) using best fit regression model. The function "lineReg" has three inputs-"marks" has scores of Mathematics and Physics in the first and second column respectively; "score" is a cell array with marks of Mathematics only in the first column and empty second column; and "k" is the order of the ploynomial used to determine best fit regression model. You need to determine three output variables- a figure hadle "fig" plotting the marks and the best fit curve; "predict" is a cell array containing both given marks of Mathematics and the predicted marks of Physics; and "coefs" with coefficients of the best fit model. For figure, plot the given marks using red stars, and the best fit curve as solid blue line with an appropriate legend. Refer to the given test case. Given test case: Mathematics = sort(randi([30 100],30,1)); Physics = sort(randi([30 100],30,1)); marks = [Mathematics, Physics]; score = {[50] []; [60] []; [70] []}; k = 2; [fig, predict, coefs] = linReg(marks,score,k)
fig = Figure (1) with properties: Number: 1 Name: ' ' Color: [0.9400 0.9400 0.9400] Position: [488 342 560 420] Units: 'pixels'
predict = 3x2 cell array {[50]} {[39.7510]} {[60]} {[45.7378]} {[70]} {[53.9462]}
coefs = 0.011 -0.6232 43.1400
You're given the following code in matlab just fill it in: function [fig, predict, coefs] = linReg(marks, score, k) x = linspace(0,100,100); % use this array to predict y for plotting fig = figure; % plot the given data points first hold on % then plot the fitting data points axis([0 100 0 100]) % axis limits end
- Assistant
function [fig, predict, coefs] = linReg(marks, score, k)
x = linspace(0,100,100); % use this array to predict y for plotting fig = figure;
% plot the given data points first plot(marks(:,1), marks(:,2), 'r*'); hold on
% Fit the data using a polynomial of order k coefs = polyfit(marks(:,1)', marks(:,2)', k);
% Calculate fitted y values y = polyval(coefs, x);
% Plot the fitting data points plot(x, y, 'b-'); legend('Given data points', 'Best fit curve');
axis([0 100 0 100]); % axis limits
% Predict Physics marks for the given Math scores predicted_physics = cellfun(@(s) polyval(coefs, s), score(:,1), 'UniformOutput', false); predict = [score(:, 1) predicted_physics];
end