size mismatch in assigning values to a matrice

1 view (last 30 days)
hey i want to run this but it keeps erroring on mismatching size for F(:,:,i):
Unable to perform assignment because the size of
the left side is 1-by-2 and the size of the right
side is 2-by-2.
and as I check the equation on F, the right side seems a 1-by-2 theoritically
clc;clear all;close all;
A=[0.9974 0.0539;-0.1078 .1591];
B=[0.0013 0.0539]';
H=zeros(2);
Q=[0.25 0;0 0.05];
R=0.05;
[K,S,e]=dlqr(A,B,Q,R);
F=zeros(1,2,200);
P=zeros(2,2,200);
for i=200:-1:2
P(:,:,1)=H;
F(:,:,1)=[0 0];
F(:,:,i)= -(B'.*P(:,:,i-1).*A).*inv(R+B'.*P(:,:,i-1).*B);
P(:,:,i)=(A+B.*F(:,:,i))'.*P(:,:,i-1).*(A+B.*F(:,:,i))+((F(:,:,i))'.*R.*F(i))+Q;
end

Answers (1)

KSSV
KSSV on 5 Dec 2021
You need to initiliaze the matrices properly.
A=[0.9974 0.0539;-0.1078 .1591];
B=[0.0013 0.0539]';
H=zeros(2);
Q=[0.25 0;0 0.05];
R=0.05;
[K,S,e]=dlqr(A,B,Q,R);
F=zeros(2,2,200); % <--- change here
P=zeros(2,2,200);
for i=200:-1:2
P(:,:,1)=H;
% F(:,:,1)=[0 0]; % <-- this is not required
F(:,:,i)= -(B'.*P(:,:,i-1).*A).*inv(R+B'.*P(:,:,i-1).*B);
P(:,:,i)=(A+B.*F(:,:,i))'.*P(:,:,i-1).*(A+B.*F(:,:,i))+((F(:,:,i))'.*R.*F(i))+Q;
end

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!