gap in the plot

Снимок.PNG
How can I make the red plot start in the same place (from 1)?

Answers (1)

red_x = red_x - min(red_x) + 1;
plot(blue_x, blue_y, 'b', red_x, red_y, 'r')

6 Comments

Valeria Pinus
Valeria Pinus on 31 Oct 2019
This has just created a gap on the other side and moved the whole plot :(
Снимок.PNG
Yes, that is to be expected. You did not specify that was not to happen.
It looks to me as if whatever process generated the data for the red line did not start from the same position. However, you have not given us any information about how the red line was generated, so we can only make wild guesses.
My bad. Here is the code:
X=[1 2 3 4 5 6 7 8 9]; %госдолг сша
Y=[120.7 26.0 162.0 50.7 256.9 290.5 380.9 90.9 20.6];
xzv=5.5;
xi=min(X):0.05:max(X); % Вспомогательный вектор абсциссы
yi=spline(X,Y,xi);
plot(X,Y,'*k',xi,yi,'b');grid;hold on; % Построение графика
AS=[2 1 0 0 0 0 0 0 0;
1 4 1 0 0 0 0 0 0;
0 1 4 1 0 0 0 0 0;
0 0 1 4 1 0 0 0 0;
0 0 0 1 4 1 0 0 0;
0 0 0 0 1 4 1 0 0;
0 0 0 0 0 1 4 1 0;
0 0 0 0 0 0 1 4 1;
0 0 0 0 0 0 0 1 2];
BS=3.*[26.0-120.7;162.0-120.7;50.7-26.0;
256.9-162.0;290.5-50.7;380.9-256.9;
90.9-290.5;20.6-380.9;20.6-90.9];
S=AS\BS
s3x=form2(X,Y,S,xzv) %значение s3(x*)
dx=(max(X)-min(X))/50; % Вычисление шага
for i=(1:50)
xi_new(i)=min(X)+dx*i; % Вспомогательные точки абсциссы
zi(i)=form2(X,Y,S,xi_new(i)); % Значение S3 в точке xi(i)
end
plot(X,Y,'*k',xi_new,zi,'r');hold off; % Построение графика
form2:
function y=form2(X,Y,S,z);
i=max(find(X<z));
h=X(i+1)-X(i);
p=(X(i+1)-z)/h;
q=(z-X(i))/h;
y=p^2*(2*q+1)*Y(i)+p^2*q*S(i)+...
q^2*(2*p+1)*Y(i+1)-q^2*p*S(i+1);
Perhaps
xi_new(i)=min(X)+dx*i;
should be
xi_new(i)=min(X)+dx*(i-1);
?
Valeria Pinus
Valeria Pinus on 31 Oct 2019
that way it says
Unable to perform assignment because the left and right sides have a different number of elements.
Error in lesson5_1 (line 24)
zi(i)=form2(X,Y,S,xi_new(i));
I see.
Your processing requires a gap. In form2 you have
i=max(find(X<z));
which would be empty if z is exatly equal to the first X because you are attempting to process right to the left margin.
You can reduce the width of the gap by reducing dx, but you cannot eliminate the gap.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2019a

Asked:

on 31 Oct 2019

Commented:

on 31 Oct 2019

Community Treasure Hunt

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

Start Hunting!