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)

 Accepted Answer

KSSV
KSSV on 2 Apr 2021
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 Comments

Meenakshi Tripathi
Meenakshi Tripathi on 2 Apr 2021
Edited: Meenakshi Tripathi on 2 Apr 2021
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?
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).

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Asked:

on 2 Apr 2021

Commented:

on 2 Apr 2021

Community Treasure Hunt

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

Start Hunting!