Hi, I have trouble writing part of the cost function. This is my optimization problem

min trace(p(k+1))
subjet to (x(k+1)-xhat(k+1))(x(k+1)-xhat(k+1))'<=p(k+1)

The p-matrix is a symmetric one that is found during optimization.
How should I define p? Due to the fact that the optimization is such that at each stage the value of p is used in other lines of the program.

I don’t know what the rest of your problem, so can’t say for sure what will work. But based on the portion shown,

variable p(n,n) symmetric % where n is whatever value is appropriate

if p is a matrix, it makes no sense to have trace(p(k+1)) as the objective function. Perhaps you want minimize(trace(p))
?

I have no idea what you really intend for the constraint. p(k+1) makes no sense. If k is used in the constraint, it needs to have a numerical value, such as if it is the index of a for loop the constraint is specified in.

is there only one p matrix? Or are there num_k matrices, one for each value of k from 1 to num_k ? And if so, are you only trying to minimize the trace of the kth matrix? if so, you might want

variable P(n,n,num_k) symmetric % each 2D slice p( :,:,k) is declared to be symmetric

And then the objective function and constraints would be in terms of P(:,:,k) for whatever the relevant value(s) of k are.

Perhaps you can carefully write out mathematically the optimization problem you want to solve. That is as much, or more, for your benefit, as for the benefit of forum readers trying to help you.

Thank you for your attention and explanation. You are right, I have to explain the optimization issue more clearly.
The optimization problem is as follows
min trace(p(k+1))
subject to (x(k+1)-xhat(k+1))(x(k+1)-xhat(k+1))’<=p(k+1)
Matrix p is one of the optimization variables. I have another part of the problem
p(k)=E(k)*E(k)’
Which E is a factorization of p.Which we usually define E(k)=sqrt(p(k)). The initial value is also defined for p. That is, at each stage of the simulation, the value of p is required for E. Finally, the plot must be drawn.
According to your explanation that you mentioned
min trace§
I made such a definition for the code I wrote, but the result was only a matrix, and therefore the plot was not derived from P.
Another point is that in the continuation of the work, because it should be extended to network systems, the p matrix is defined as follows
P(k+1)=blkdiag(pi(k+1)),i=1,2,…,n
I hope I have been able to say what I mean.

ok
I have trouble defining my cost performance and need help with that.
The p diagonal matrix contains several p matrices, for example 3.
In my programming, I specify the length of the for loop and the number p inside the diagonal matrix with the subtitle i, which, as I said, can be three.