matlab 尝试创建一个具有5个子图的图形,这些子图是一个变量的迭代,以1为步长变化-为什么它不起作用?

guykilcj  于 2023-08-06  发布在  Matlab
关注(0)|答案(1)|浏览(211)
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

字符串
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
当我尝试使用i索引时,它返回“对于这种类型的变量不支持括号索引。我只是试图创建一个图与5个不同的子图是一个迭代的一个变化的变量在一个方程。

55ooxyrt

55ooxyrt1#

首先,错误发生在任何绘图发生之前,所以即使提到绘图也是一个红色的鲱鱼。
大括号索引是MATLAB用于单元数组的索引,与python中的普通列表非常相似。它可用于存储任意对象。它可以是包含数组数据的不同对象

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

字符串
然而,在这段代码中没有创建任何形式的单元数组,所以不应该期望用这样的数组索引任何东西。
很难从代码中缺乏适当的缩进来判断,但是gamma循环几乎包含了整个代码,并且premium只是一个临时值,它只为当时所需的一个图计算。
gamma循环中,有一个嵌套循环,同样包含5个值。你的意思是这些和gamma一样吗?如果是这样的话,那么应该取出内部循环,将i替换为gamma,并将figure; hold on放置在外部循环之外。
或者,您可以实际结束第一个循环,将实际的premium值存储到单元数组中。

premiums = {}
for gamma = 1:5
   ...
   premiums{gamma} = premium
end

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

相关问题