Matrix riccati differential equation using matlab

In summary, the conversation discusses a code that solves a matrix differential equation backwards using three different functions. The code is meant to make the error (e) converge to zero and the contract (C) converge to 1. However, there seem to be errors in the three functions used, specifically with the use of the reshape function. The expert suggests checking the code for errors and providing more information if needed.
  • #1
Jeffrey Eiyike
8
0
aassignment.png

Homework Statement

Homework Equations

The Attempt at a Solution

 
Physics news on Phys.org
  • #2
I can upload the code if you want to see my attempt.
 
  • #3
Jeffrey Eiyike said:
I can upload the code if you want to see my attempt.
Yes, please do. It's best if you post it inline using code tags. It should look like this:
[code]
<< your MATLAB code>>
[/code]
 
  • #4
Code:
function dPdt = mRiccati(t,P, B, Q, R)
%computes Phi
  P = reshape(P, size(Q)); % Convert from "n^2"-by-1 to "n"-by-"n"
  dPdt = -Q + P' * B * R^-1 * B.' * P; % Determine derivative
  dPdt = dPdt(:); % Convert from "n"-by-"n" to "n^2"-by-1 or converting to column vector
end

function dhdt = mRiccati2(t, P0, B, h, R)
%compute h
  h = reshape(h, size(B)); % Convert from "n^2"-by-1 to "n"-by-"n"
  dhdt = 0.5* P0' * B * R^-1 * B.' * h; % Determine derivative
  dhdt = dhdt(:); % Convert from "n"-by-"n" to "n^2"-by-1
endfunction dXdt = mRiccati3(t,X, h0, P0, B, R, sigma)
% Compute Chi
  X = reshape(X, size(R)); % Convert from "n^2"-by-1 to "n"-by-"n"
  dXdt = (1/2)* h0' * B * R^-1 * B.' * h0- ((sigma^2)/2)*trace(-P0) ; % Determine derivative
  dXdt = dXdt(:); % Convert from "n"-by-"n" to "n^2"-by-1
end

clear all; close all
% sigma=[1 1 1 0.5 0.5 0.5 0.33];
sigma=[1 1 1 1.4142 1.4142 1.4142 1.7321];
q=[1 1 1 1 1 1 1]; 
C=[5; 5; 5; 10; 10; 10; 15];
e=[4; 3; 2; 2; 2; 2; 3];

    dt=0.1;        %step size
    B = [1; 1];    % Input vector Matrix
     R = 1;          %penalty on the optimal  U that updates the contract
    N = [0 3]; % Final condition Linear term
    t_end = 20;
    tspan = t_end:(-0.1):0;
%   
%Solves the Matrix Differential equation Backwards

for ii=1:length(sigma);%:length(sigma)    Q = [q(ii) 0; 0 0]; %Final condition on the quadratic term
  
%Solution for the first Matrix Differential equation
[T1 P] = ode45(@(t,P)mRiccati(t,P, B, Q, R), tspan, Q);

    for i= 1:length(P)
        P0 = reshape(P(i,:), size(Q));

%Solution for the second Matrix Differential equation (Phi)
        [T2, h] = ode45(@(t,h) mRiccati2(t, P0, B, h, R), tspan, N);% obtain matrix P from last entry (initial value)(h)

    h0 = reshape(h(i,:), size(B));

%Solution for the Third Matrix Differential equation(X)
    [T3, X] = ode45(@(t,X) mRiccati3(t,X, h0, P0, B, R, sigma(ii)), tspan, 0);
    end
  
% Flips the parameter
h=flipud(h);   
P=flipud(P);
X=flipud(X);

% Compute the value function, error and contract integrand
    for j= 1: length(P)
%         display(num2str(length(P)))
        ZZ= [e(ii, j)  C(ii, j)]; % State
        V(ii, j)= -0.5* ZZ*reshape(P(j,:), size(Q))*ZZ' + reshape(h(j,:), size(B))'*ZZ'- X(j);  % Value function
        U(ii, j)= inv(R)*B'*(-reshape(P(j,:), size(Q))*ZZ'+ reshape(h(j,:), size(B)));
        C(ii, j+1) =  C(ii, j) + U(ii, j)*dt;    %Contract
        e(ii, j+1)=e(ii, j)+ U(ii, j)*dt ;%-sigma(ii)*sqrt(dt)*normrnd(0,1);   %error     
    end
 
Last edited by a moderator:
  • #5
So does your code work or are you having problems with it? If there are problems or errors, tell us about them. Don't make us guess at what might be going wrong.
 
  • #6
My error e is surpose to converge to zero an remain there and the contract C is suppose to converge to 1 and remain there. But mine converges and later starts increasing
 
  • #7
Your three functions don't seem to me to be working correctly.
Matlab:
function dPdt = mRiccati(t,P, B, Q, R)
%computes Phi
P = reshape(P, size(Q)); % Convert from "n^2"-by-1 to "n"-by-"n"
...
If Q is an n2 by 1 vector, then size(Q) evaluates to [n2, 1], not [n, n]. See size in the MATLAB documentation. The call to the reshape function doesn't actually change P, as far as I can see. The same problem exists for your other three functions. There might be other problems, but this is the first one I noticed.

Where is the code for the mRiccati function? It doesn't appear to be something provided by MathWorks, but I could be wrong.
 

Related to Matrix riccati differential equation using matlab

1. What is a Matrix Riccati differential equation?

A Matrix Riccati differential equation is a type of differential equation that involves a matrix function and its derivative. It is commonly used in control theory and system analysis to model the evolution of a system over time.

2. How is MATLAB used to solve Matrix Riccati differential equations?

MATLAB is a popular programming language that is commonly used to solve mathematical problems, including Matrix Riccati differential equations. It has built-in functions and tools for solving differential equations numerically, making it a powerful tool for scientists and engineers.

3. What are the advantages of using MATLAB for solving Matrix Riccati differential equations?

One of the main advantages of using MATLAB is its versatility and efficiency in solving complex mathematical problems. It also has a user-friendly interface, making it accessible to both experts and beginners. Additionally, MATLAB has a large community of users and resources, making it easier to troubleshoot and find solutions to problems.

4. What are the limitations of using MATLAB for solving Matrix Riccati differential equations?

Although MATLAB is a powerful tool for solving Matrix Riccati differential equations, it does have some limitations. One of the main limitations is the high cost of the software, which may be a barrier for some users. Additionally, MATLAB may not be the best tool for solving very large or complex systems, as it may require a significant amount of memory and computing power.

5. Can MATLAB be used to solve other types of differential equations?

Yes, MATLAB can be used to solve a wide range of differential equations, including ordinary differential equations, partial differential equations, and stochastic differential equations. It also has the capability to handle both linear and nonlinear equations. However, the specific methods and techniques for solving these equations may vary.

Similar threads

  • Calculus and Beyond Homework Help
Replies
5
Views
988
  • Calculus and Beyond Homework Help
Replies
1
Views
667
  • Calculus and Beyond Homework Help
Replies
7
Views
427
  • Calculus and Beyond Homework Help
Replies
8
Views
2K
  • Calculus and Beyond Homework Help
Replies
10
Views
535
Replies
5
Views
2K
  • Calculus and Beyond Homework Help
Replies
1
Views
386
  • Calculus and Beyond Homework Help
Replies
1
Views
367
  • Calculus and Beyond Homework Help
Replies
6
Views
368
  • Calculus and Beyond Homework Help
Replies
2
Views
297
Back
Top