Question

I need this program in MATLAB with different or another code. INPUT: x = [0 1...

I need this program in MATLAB with different or another code.

INPUT:

x = [0 1 8 12 27];
y = [1 2 3 4 5];


nx = length(x);
ny = length(y);
n = length(x);
if nx ~= ny
display('Error. La cantidad de datos en x no es igual que los datos en y')
end

Sx = sum(x);
Sxx = sum(x.^2);
Sy = sum(y);
Sxy = sum(x.*y);

a1 = (n*Sxy - Sx*Sy)/(n*Sxx-(Sx)^2);
a0 = (Sxx*Sy - Sxy*Sx)/(n*Sxx - (Sx)^2);

m = a1; b = a0;
f = m*x + b;

plot(x,y,'or',x,f,'b')
title('y vs x')
legend('data','regression','Location','SouthOutside')
xlabel('x')
ylabel('y')
display('The values of y in the linear regression is:')
disp(f)

OUTPUT:

The values of y in the linear regression is:
1.6924 1.8286 2.7821 3.3269 5.3701

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have given you two versions of the above code. You can use any one of them.

MATLAB Code

%Data points
x=[0 1 8 12 17];
y=[ 1 2 3 4 5];
%Comparing lengths of x and y
if (length(x)~=length(y))
error('X and Y are of different lengths. Please Check!!!')
else
n=length(x);
end
sx=sum(x);
sx2=sum(x.^2);
sy=sum(y);
sxy=sum(x.*y);
%calculaitng 'm' and 'b'
m=(n*sxy-sx*sy)/(n*sx2-sx^2);
b=(sx2*sy-sxy*sx)/(n*sx2-sx^2);
%y-estimate calculaiton
y_hat=m*x+b;
%Plotting the actual data and estimat
scatter(x,y);
hold on
plot(x,y_hat)
xlabel('x')
ylabel('y')
title('x vs.y')
legend({'Data','Estimate'},'Box','off','Location','northwest')
fprintf('The values of y obtained with Linear Regression is ')
fprintf('%1.4f %1.4f %1.4f %1.4f %1.4f ',y_hat)

Output on the command window

The values of y obtained with Linear Regression is
1.3652 1.5803 3.0860 3.9465 5.0220

MATLAB Code - version 2

The formulas used for 'm' and 'b' are

%Data points
x=[0 1 8 12 17];
y=[ 1 2 3 4 5];
%Comparing lengths of x and y
if (length(x)~=length(y))
error('X and Y are of different lengths. Please Check!!!')
else
n=length(x);
end
mu_x=mean(x);
mu_y=mean(y);
mu_xy=mean(x.*y);
mu_x2=mean(x.^2);
%calculaitng 'm' and 'b'
m=(mu_x*mu_y-mu_xy)/(mu_x^2-mu_x2);
b=mu_y-m*mu_x;
%y-estimate calculaiton
y_hat=m*x+b;
%Plotting the actual data and estimat
scatter(x,y);
hold on
plot(x,y_hat)
xlabel('x')
ylabel('y')
title('x vs.y')
legend({'Data','Estimate'},'Box','off','Location','northwest')
%Displaying the values of estimates on command window
fprintf('The values of y obtained with Linear Regression is ')
fprintf('%1.4f %1.4f %1.4f %1.4f %1.4f ',y_hat)

Output on the command window

The values of y obtained with Linear Regression is
1.3652 1.5803 3.0860 3.9465 5.0220

Add a comment
Know the answer?
Add Answer to:
I need this program in MATLAB with different or another code. INPUT: x = [0 1...
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
  • Write a JavaScript program to implement the Least-Squares Linear Regression algorithm shown below, for an arbitrary...

    Write a JavaScript program to implement the Least-Squares Linear Regression algorithm shown below, for an arbitrary set of ( x, y ) data points entered by the user. The Algorithm: 1. Read in a series of ( x, y ) data pairs. While doing so, calculate the following sums: • N = number of data pairs. • Sx = ∑ x = Summation ( total ) of all X values. • Sy = ∑ y = Summation ( total )...

  • by using matlab linreg() function file given below On average, the surface area A of human...

    by using matlab linreg() function file given below On average, the surface area A of human beings is related to weight W and height H. Measurements of several individuals of height 180cm are provided below. It is believed that the following data can be modelled using an exponential equation A = aeBW W (kg) 70 2.1 75 2.12 77 2.15 80 2.20 82 2.22 84 2.23 87 2.26 90 2.30 A (m2) A. Show by hand with pen and paper...

  • MATLAB code starts here --------- clear T0=2; w0=2*pi/T0; f0=1/T0; Tmax=4; Nmax=15; %--- i=1; for t=-Tmax: .01:Tmax...

    MATLAB code starts here --------- clear T0=2; w0=2*pi/T0; f0=1/T0; Tmax=4; Nmax=15; %--- i=1; for t=-Tmax: .01:Tmax T(i)=t; if t>=(T0/2) while (t>T0/2) t=t-T0; end elseif t<=-(T0/2) while (t<=-T0/2) t=t+T0; end end if abs(t)<=(T0/4) y(i)=1; else y(i)=0; end i=i+1; end plot(T,y),grid, xlabel('Time (sec)'); title('y(t) square wave'); shg disp('Hit return..'); pause %--- a0=1/2; F(1)=0; %dc freq C(1)=a0; for n=1:Nmax a(n)=(2/(n*pi))*sin((n*pi)/2); b(n)=0; C(n+1)=sqrt(a(n)^2+b(n)^2); F(n+1)=n*f0; end stem(F,abs,(C)), grid, title(['Line Spectrum: Harmonics = ' num2str(Nmax)]); xlabel('Freq(Hz)'), ylabel('Cn'), shg disp('Hit return...'); pause %--- yest=a0*ones(1,length(T)); for n=1:Nmax yest=yest+a(n)*cos(2*n*pi*T/T0)+b(n)*sin(2*n*pi*T/T0);...

  • Need help converting the following code from Matlab into Python: N=2048; fs=4.9; t=0:1/fs:(N-1)/fs; fs1=200; t1=0:1/fs1:(N-1)/fs1; x2=0.5+0.6366.*cos(2.*pi.*t1)+0.1273.*cos(10.*pi.*t1)-0.0909.*cos(14.*pi.*t1);...

    Need help converting the following code from Matlab into Python: N=2048; fs=4.9; t=0:1/fs:(N-1)/fs; fs1=200; t1=0:1/fs1:(N-1)/fs1; x2=0.5+0.6366.*cos(2.*pi.*t1)+0.1273.*cos(10.*pi.*t1)-0.0909.*cos(14.*pi.*t1); x=0.5+0.6366.*cos(2.*pi.*t)+0.1273.*cos(10.*pi.*t)-0.0909.*cos(14.*pi.*t); X=fftshift(fft(x)); f=linspace(-fs/2,fs/2,N); plot(f,abs(X)./N); xlabel('f'); ylabel('|F(f)|'); title('magnitude spectrum of sampled signal'); x1=ifft(fftshift(X)); figure plot(t(1:100),x1(1:100)); xlabel('t'); ylabel('f(t)'); title('f(t) obtained by inverse transform'); figure plot(t1(1:1000),x2(1:1000)); xlabel('t'); ylabel('f(t)'); title('original f(t)');

  • Please i need final report on this program. thanks % Preparation of Data tempApril1969 = [36.5 51.5 41.5 47.5 56 50.5 54...

    Please i need final report on this program. thanks % Preparation of Data tempApril1969 = [36.5 51.5 41.5 47.5 56 50.5 54.5 55 51 61 54 48 50 54 57.5 57.5 64 57 50.5 47.5 47 50.5 52 48 56 65.5 63.5 72 61 54.5]; % temperature data of April, 1969 (shape = 1x30) tempApril2019 = [39.5 43.5 54 51.5 42 52.5 55 63 46.5 51 46 55.5 66.5 63 53.5 54 54 56.5 65.5 58.5 57.5 54.5 64.5 61...

  • For MATLAB 3. write a program to plot a scatter plot of data (x, y) pairs...

    For MATLAB 3. write a program to plot a scatter plot of data (x, y) pairs and compute the correlation coefficient. Data and details are provided below. In Lecture 9 it was noted that the numerator used in the sample variance could be obtained using the sum(x) and sum(x. 'x) functions: iz1 The average is sum(x)/n. If an array y of the same length is computed in the same way call that term Syy. The term Sxy can be computed...

  • the code in the photo for this I.V.P dy/dx= x+y. y(0)=1 i need the two in the photo thank you New folder Bookmark...

    the code in the photo for this I.V.P dy/dx= x+y. y(0)=1 i need the two in the photo thank you New folder Bookmarks G Google dy/dx x+y, y(0)=1 2 h Exact Solution 1.8 Approximate Solution Mesh Points 1.6 -Direction Fied 1.4 1.2 1 0.8 04 0.2 0.3 0.1 0 X CAUsersleskandara\Desktop\New folder emo.m EDITOR PUBLISH VEW Run Section FILE NAVIGATE EDIT Breakpoints Run Run and FL Advance Run and Advance Time BREAKPOINTS RUN 1 - clear all 2 clc 3-...

  • This is a MATLAB Question. Below is my base code for a Fourier Series of a...

    This is a MATLAB Question. Below is my base code for a Fourier Series of a half triangle wave. So I am being asked to isolate the first 8 components so that only those first 8 components of the half triangle function are calculated by MATLAB fft function, and then afterwards I am asked to do the same thing but with the first 20 components rather than the first 8. How do I isolate the first x number of components...

  • I NEED A SOLUTION ASAP PLEASE My Notes Ask Your Teacher Values of modulus of elasticity (MOE, the ratio of stress, .e., force per unit area, to strain, .e., deformation per unit length, in GPa) and fl...

    I NEED A SOLUTION ASAP PLEASE My Notes Ask Your Teacher Values of modulus of elasticity (MOE, the ratio of stress, .e., force per unit area, to strain, .e., deformation per unit length, in GPa) and flexural strength (a measure of the ability to resist failure in bending, in MPa) were determined for a sample ㅁfconcrete beams cf a !rtein typer resulting in the following data MOE 97 33.1 33.8 35.3 35.7 36.2 36.3 36.4 37.7 37.7 3B.6 38.6 39.6...

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