MATLAB Answers

Why my plot is not working?

1 view (last 30 days)
I have a problem with my plot. When I do plot(R). This picture will be shown
and when I do plot(y), this picture will be shown
I hope, when I do plot(y,R), this picture will be shown
but. When I try do plot(y,R). The picture show different like this
How to fix it?
This is my code
clc;
clear all;
T=readtable('polinom.xlsx');
xa=T.x;
ya=T.y;
x=transpose(xa);
y=transpose(ya);
n =length(x);
my=sum(y);
m = sum(bsxfun(@power,x(:),1:(2*n)),1);
myy = sum(bsxfun(@power,y(:),1:(2*n)),1);
xv = x(1:n);
yv = y(1:n);
ny = sum(bsxfun(@times,bsxfun(@power,xv(:),1:99),yv(:)),1);
for na=2:83
for i=1:na
for j=1:na
for k=1:na
A(1,1)=n;
if i>1
A(i,1)=m(i-1);
end
if k>1
A(i,k)=m(k-1);
if A(i,k)==A(i,k-1)
A(i,k)=m(k);
elseif A(i,k)<A(i,k-1)
A(i,k)=m(i+k-2);
end
end
end
end
end
B=[my 1];
for i=2:na
b = ny(i-1);
B(i)=b;
end
for k=1:na-1
for i=k+1:na
if A(i,k)~=0
lambda=A(i,k)/A(k,k);
for j=1:na
A(i,j)=A(i,j)-lambda*A(k,j);
end
B(i)=B(i)-lambda*B(k);
end
end
end
for i=na:-1:1
sum=0;
X(i)=0;
for j=1:na
sum=sum+A(i,j)*X(j);
end
X(i)=(B(i)-sum)/A(i,i);
end
for c = 1:n
R(c) = 0;
for d = 1:na
R(c) = R(c)+X(d)*x(c)^(d-1);
end
end
na;
R;
error=abs(y-R);
t=0;
for s = 1:length(error)
t= t + error(s);
end
t;
ta(na)=t;
end
ta(1)=300;
ta;
xc=min(ta);
find(xc);
figure(100)
plot(y,R)

Accepted Answer

Sriram Tadavarty
Sriram Tadavarty on 19 Mar 2020
Hi Eddy,
To get the desired plot, you can perform the following
% Option 1: To use it with the appropriate indexing
plot(1:length(y),y,1:length(R),R)
% Option 2: To use the hold on
plot(y)
hold on
plot(R)
The issue with using plot(y,R) is that it takes the first argument as x-axis and is R is plotted as a function of y. But, that is not what you are looking for.
Hope this helps.
Regards,
Sriram
  1 Comment
Eddy Iswardi
Eddy Iswardi on 19 Mar 2020
thank you very much

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!