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

답변 (1개)

Walter Roberson
Walter Roberson 2019년 10월 31일

0 개 추천

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

댓글 수: 6

Valeria Pinus
Valeria Pinus 2019년 10월 31일
This has just created a gap on the other side and moved the whole plot :(
Снимок.PNG
Walter Roberson
Walter Roberson 2019년 10월 31일
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.
Valeria Pinus
Valeria Pinus 2019년 10월 31일
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);
Walter Roberson
Walter Roberson 2019년 10월 31일
Perhaps
xi_new(i)=min(X)+dx*i;
should be
xi_new(i)=min(X)+dx*(i-1);
?
Valeria Pinus
Valeria Pinus 2019년 10월 31일
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));
Walter Roberson
Walter Roberson 2019년 10월 31일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

릴리스

R2019a

태그

질문:

2019년 10월 31일

댓글:

2019년 10월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by