Search code examples
matlabfor-loopiterationsubplotfigure

Trying to Create one figure with 5 subplots that are an iteration of one variable changing in steps of 1 - Why is it not working?


clear all; close all; clc;

%% parameters

rho = 1;
k0 = 1;
K = 1;
y1 = 0.1;
for gamma = 1:5;
mu = 0.06;
mu0 = 0.06;
sigma0 = 0.15;
sigmae = 0.1;
sigmaS = 0.1;
beta = 1-(1-0.04)/252;
sigmaS = linspace(0.05,0.5,100);
precision_S = 1./sigmaS.^2;


mbar_plus = mu0+0.01; % assuming sentiment increases 0.5% after announcement
mbar_minus = mu0;
sigmahat_plus = sqrt(1./(1/sigma0^2+1/sigmae^2+1./sigmaS.^2));
sigmahat_minus = sqrt(1/(1/sigma0^2+1/sigmae^2));
varm_minus = (1/sigmae^2)./(1/sigma0^2+1/sigmae^2)^2;
varm_plus =  (1/sigmae^2)./(1/sigma0^2+1/sigmae^2+1./sigmaS.^2).^2;

%%%%%%% solution assuming UNSCHEDULED announcement
%% announcement premium vs. sigmaS
%% premium 1 is the pure disagreement term 
%% premium 2 is the interaction term of disagreement and sentiment, assuming sentiment = true
p_plus = beta*(mbar_plus - gamma*sigmahat_plus.^2*K).*exp(-varm_plus./(2.*sigmahat_plus.^2)-(mbar_plus - y1).*gamma.*K + gamma.*sigmahat_plus.^2.*K^2./2);
R_plus= (mbar_plus - gamma.*sigmahat_plus.^2*K)./p_plus;
p_minus = beta*(mbar_minus - gamma*sigmahat_minus.^2*K).*exp(-varm_minus./(2.*sigmahat_minus.^2)-(mbar_minus - y1).*gamma.*K + gamma.*sigmahat_minus.^2.*K^2./2);
premium_1 = (mbar_plus - gamma.*sigmahat_plus.^2.*K)./(mbar_plus - gamma.*sigmahat_minus.^2.*K);
premium_2 = (mbar_plus-gamma.*sigmahat_minus.^2*K)./(mbar_minus-gamma.*sigmahat_minus.^2*K).*exp(-(mbar_plus-mbar_minus).*gamma*K);
premium = premium_1.*premium_2;

%% plot

    figure;
    hold on
    for i = 1:5
            subplot(1,5,i);
            plot(precision_S,premium{i},'Linewidth',2);
            title(['Announcement premium - Gamma=' , num2str(gamma)])
            xlabel('1/\sigma_S^2')
            ylabel('p^+/p^-')
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

When I try and index with i, it returns "Brace indexing is not supported for variables of this type. I'm simply trying to create one figure with 5 different subplots that are an iteration of one changing variable in an equation.


Solution

  • To start, the error happens before any plotting can occur, so even mentioning plotting is a red herring.

    Brace indexing is what MATLAB uses for cell arrays, quite similar to the plain list in python. It can be used to store arbitrary objects. It could be different objects with data for your array

    premiums = {[1,2,3], [4,5,6,7,8], [9,10,11,12]};
    premiums{1}   % our first dataset
    

    However, there is nothing in this code that creates any form of cell array, so there should not be any expectation that to index anything with such an array.

    Hard to tell from the lack of proper indentation in your code, but your gamma loop encompasses almost the entire code, and your premium is just a temporary value which is computed for just 1 of the desired plots at the time.

    Inside the gamma loop, you have a nested loop, again over 5 values. Did you perhaps mean for these to be the same as gamma? If so, then your inner loop should be taken out, i replaced with gamma and figure; hold on be placed outside the outer loop.

    Alternatively, you could actually end the first loop, storing actual premium values into a cell array.

    premiums = {}
    for gamma = 1:5
       ...
       premiums{gamma} = premium
    end
    
    figure;
    hold on
    for gamma = 1:5
        subplot(...);
        plot(precision_S, premiums{gamma})
        ...
    end