Question

I need to change the MATLAB code below to something else, but I need the solutions...

I need to change the MATLAB code below to something else, but I need the solutions to match each other. (Also try not to copy answer posted on chegg already)

%
% Problem 1.8
%

clc; clear all; close all; % reset matlab w

orthregdata; % load data

n = length(a);

w = ones(n,1);
ma = w'*a/n;
mb = w'*b/n;
% ma = mean(a);
% mb = mean(b);

sa = norm(a-ma)/sqrt(n);
sb = norm(b-mb)/sqrt(n);
% sa = std(a,1); % Pass in '1' to use the nodefault normaliztion by n
% sb = std(b,1);

rho = 1/n*(a-ma)'*(b-mb)/(sa*sb); % corrcoef(a,b)

if (abs(rho)>1e-8)
c2 = (-(sa/sb-sb/sa)+sqrt((sa/sb-sb/sa)^2+4*rho^2))/(2*rho);
else
c2 = 0;
end

c1 = mb-ma*c2;

figure;

plot(a,b,'*');
hold on;

x = min(a):max(a);
plot(x,c1+c2*x,'linewidth',2);

% leat sqaures

c2_l = rho*sb/sa;
c1_l = mb-ma*c2_l;

hold on;
plot(x,c1_l+c2_l*x,'linewidth',2);
grid on;

legend 'Data Points' 'Orhtogonal Distance Regression Fitting' 'least Squares Fitting'

Homework Answers

Answer #1

I have changed the above code to the maximum

clc
clear all
close all
%
% Problem 1.8
%

clc; clear all; close all; % reset matlab w

%orthregdata; % load data
a=1:10;
n = numel(a);

w = ones(n,1);
ma = transpose(w)*a/n;
mb = transpose(w)*b/n;
% ma = mean(a);
% mb = mean(b);

sa = norm(a-ma)/(n)^0.5;
sb = norm(b-mb)/(n)^0.5;
% sa = std(a,1); % Pass in '1' to use the nodefault normaliztion by n
% sb = std(b,1);

rho = 1/n*transpose((a-ma))*(b-mb)/(sa*sb); % corrcoef(a,b)

if (abs(rho)>1e-8)
c2 = (-(sa/sb-sb/sa)+((sa/sb-sb/sa)^2+4*rho^2).^0.5)*0.5/(rho);
else
c2 = 0;
end
figure(1);
hold on;
plot(a,b,'*');
x = min(a):max(a);
plot(x, (mb-ma*c2)+c2*x,'linewidth',2);
plot(x,(rho*sb/sa)+(mb-ma*c2_l)*x,'linewidth',2);
grid on;
legend('Data Points','Orhtogonal Distance Regression Fitting','least Squares Fitting')

Know the answer?
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
Similar Questions