Question

P2) Write a MATLAB function myLinReg Username that is defined below. Use the in-built MATLAB function sum( ) to ease your pro
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Required Matlab code with explanatory comments is given below:

{\color{Red} \text{START CODE}}

function [coeffVec,r2] = Q_myLinReg(xi,yi)
%FINDS THE BEST FIT LINE PARAMETERS AND R-SQUARED VALUE
slope=sum((xi-mean(xi)).*(yi-mean(yi)))/sum(( xi-mean(xi)).^2); %formula for slope
intercept=mean(yi)-slope*mean(xi); %formula for intercept
coeffVec = [intercept,slope];
r = sum((xi-mean(xi)).*(yi-mean(yi)))/sqrt(sum((xi-mean(xi)).^2)*sum((yi-mean(yi)).^2)); %r valu
r2=r^2; %r squared value
end

{\color{Red} \text{END CODE}}

Sample usage:

> [coeff,rsql-o myLinReg ([1,2,3], 04,5,5]) coeff = 3.6667 0. 0.5000 AKI 0.7500

Add a comment
Know the answer?
Add Answer to:
P2) Write a MATLAB function myLinReg Username that is defined below. Use the in-built MATLAB func...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT