error in plotting graph

1 回表示 (過去 30 日間)
Meenakshi Tripathi
Meenakshi Tripathi 2021 年 4 月 2 日
コメント済み: KSSV 2021 年 4 月 2 日
function homework
clc
clear all
global A B C L x0 tspan
A = [0 1;0 0];
B = [0;1];
C = [1 0];
L = [1.8750;2.6250];
tspan = [0:.1:10];
x0 = [1 0];
[x,y] = solbasic
z0 = [10 11]
z(1,:)=[10 11];
z1=z(1,:); %gives the first row
z2=z1
for i = 1:100;
t_temp =((i-1)/10):.05:(i/10);
[~,z]=ode45(@observer,t_temp,z(end,:))
z1=[z1;z(end,:)]
z2=[z2;z(2,:);z(end,:)]
end
function dzdt = observer(t,z);
dzdt=(A-L*C)*z+L*[(y(i,:)+y(i+1,:))/2]'+B*[t^2];
end
y_cap = z1*C';
e=x-z1;
figure(1)
plot(tspan,x(:,1),'r',tspan,y_cap(:,1),'b--','LineWidth',2.5)
xlabel({'Time t';'(a)'}, 'FontWeight','b','FontSize',14)
ylabel('First State', 'FontWeight','b','FontSize',14)
leg1 =legend('True x(1)','Estimated x(1)')
set(leg1,'FontSize',16);
figure(2)
plot(tspan,x(:,2),'r',tspan,y_cap(:,2),'b--','LineWidth',2.5)
xlabel({'Time t';'(b)'}, 'FontWeight','b','FontSize',14)
ylabel('Second State', 'FontWeight','b','FontSize',14)
leg2=legend('True x(2)','Estimated x(2)')
set(leg2,'FontSize',16);
end
function [x y] = solbasic()
global A B C x0 tspan u
[~, x] = ode45(@basic, tspan, x0)
y=x*C'
end
function dxdt = basic(t,x)
A = [0 1;0 0];
B = [0;1];
dxdt=A*x+B*[t^2]
end
Error that i am having is-
Index in position 2 exceeds array bounds (must not exceed 1).
Error in homework (line 38)
plot(tspan,x(:,2),'r',tspan,y_cap(:,2),'b--','LineWidth',2.5)

採用された回答

KSSV
KSSV 2021 年 4 月 2 日
Replace the line:
plot(tspan,x(:,2),'r',tspan,y_cap(:,2),'b--','LineWidth',2.5)
with
plot(tspan,x(:,2),'r',tspan,y_cap,'b--','LineWidth',2.5)
  2 件のコメント
Meenakshi Tripathi
Meenakshi Tripathi 2021 年 4 月 2 日
編集済み: Meenakshi Tripathi 2021 年 4 月 2 日
Thank you!!!
could you please explain why you did this? Also when i did the same , my graph is not converging. what should be done in that case?
are you aware of simulation of observer design related problem?
KSSV
KSSV 2021 年 4 月 2 日
Check the dimensions of each input you have given to plot. The dimensions of y_cap is 101X1, so you cannot use y_cap(:,2).

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by