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 CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by