I programmed the same model in Dynare and Matlab. The model is the Hybrid NKM under Commitment.
The plots and outputs are equal but there is a difference in the loss.

Dynare computes the exact theoretical variance from the linear approximation, but in Matlab you compute empirical moment. This will introduce some differences

In Matlab you compute empirical second moment but assume that first moment (the mean) is zero which is the theoretical mean

you get two value because your definition of data implies two columns: an index and a random value

Index in position 1 exceeds array bounds (must not exceed 1).
Error in Loss_fun (line 4)
vol_pi = (mean(data.v_solution(2,:).^2));
Error in COMMITMENT (line 98)
[ LossVal,vol_pi, vol_x ] = Loss_fun( data,w_pi, w_x )

But it does not work. I want matlab to take the mean of pi and x from a simulation lengt T=1000. My code for the model also include these simulation lenght. So, I wanted to find a way to give the input argument of T=1000. But I think this randn part is wrong because the data is not random. That was my intention with data struct

Sorry, I checked it again.
Now I think I dont need this part because, for the first row iam taking the numbers 1 to 1000 and in the second row iam taking 1000 random numbers of the normal distribution.
So no need for this line. I think it makes no sense anymore.

data = struct(‘v_solution’, [1:1000; randn(1, 1000)]);

I think I should expand my period lenght too.
My period lenght is defined in my main script with T=1000.
I think I should make it much larger.
These are my approaches to get the correct code and loss.

I increased the number of T and my loss is 3.7500e+11 instead of 13,…
The model which iam trying to replicate is the peformance of monetary policy rules in case of stochastic cost-push shocks
I think in my code iam only doing it for one single shock in one period, so I need to programm ias in every new period there is a new cost push shock.
Is this a correct approach and explanation for the differences in loss?

I try again to explain what Iam doing here and I want as a result.
At first this is my matlab code for the hybrid NKM model. COMMITMENT.m (3.1 KB)
I did the same model in dynare, where I got the finally loss.
I want to do exaclty the same as I did in dynare.
My simulation length for the model is T=100000. This T is defined in the main script as in the input argument for the lossfunction. I want to find the loss of this model, when the economy is faced by cost shocks. These shocks are happening in every new period.

Input Arguments:

data = struct(‘v_solution’,[1,T;2,T]);

here we have the v_solution as a 2 by 1 vector with the predetermined variables x and pi.
I want Matlab for x to take the first row till T (1,T) and for pi the second row till T (2,T).

Thanks this is much easier!!
I run your code and my loss is:

LossVal =
1.1611e-05

I still have the difference between the loss in dynare and matlab.
The result of dynare is :
L= 13.2610

The Matlab main script is correct too.
As I understood the model there are cost push shocks in every period. My result in matlab should be the loss for a single impulse now.
Is this correct?
I want the loss for a random shock in every new period.
How can I integrate this in my simulation?

What I do not understand is:
I computed this model in dynare too without considering that the shock hits every period.
which part of my dynare commitment code/ or of your sample from the forum is considering this detail?
Or is my dynare Code also not complete?
I suggest that I also ignored this information in my dynare Code?

I tried to find some examples to see how others simulated the model when a shock hits each period.
But unfortunately I havent find anything.
Do you know where I can find a example or a hint how to handle it?

But you have all the necessary pieces in place. Instead of simulation an IRF with a shock at time zero, you need to have a shock in every period. That can be easily added in your loop.