Iam getting the following ERRO: Array indices must be positive integers or logical values on the following code.
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
winfrida mwigilwa
il 18 Feb 2025
Modificato: Cris LaPierre
il 18 Feb 2025
mu=0.02;
betaj=0.2;
betak=0.3;
r01=0.1;
r02=0.4;
r03=0.7;
r04=1.0;
T=10;
t=0:2:20;
kappa=0.01;
sigma=1.3;
rho=0.5;
rho12=0.5;
rho13=0.5;
rho23=0.5;
rho14=0.5;
rho24=0.5;
rho34=0.5;
varthetaj=0.1;
varthetak=0.2;
aj=0.5;
ak=0.6;
bj=1.1;
bk=1.2;
vt=10;
Kj=0.5;
Kk=0.7;
q1(t)=((exp(-r01.*(T-t))).*( betaj.*(1-Kj.*Kk))).*(((-4.*aj.*varthetaj.*(1+2.*rho.*sqrt(1-rho.^2).*rho12))./(bj.^2.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2)))-( (2.*(mu-r01).*sqrt(vt).*(rho14.*sqrt(1-rho.^2)+rho.*rho24))./ (bj.*vt.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))+((sigma.*sqrt(vt).*exp(-kappa.*(T-t)).*( (4.*rho34.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-2.*(sqrt(1-rho.^2).*rho14+rho.*rho24).*(sqrt(1-rho.^2).*rho13+rho.*rho23))./(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))./(bj)))))+((exp(-r01.*(T-t)).*Kj).*( betak.*(1-Kj.*Kk))).*(((-4.*ak.*varthetak.*(1+2.*rho.*sqrt(1-rho.^2).*rho12))./(bk.^2.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2)))-( (2.*(mu-r01).*sqrt(vt).*(rho14.*sqrt(1-rho.^2)+rho.*rho24))./ (bk.*vt.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))+((sigma.*sqrt(vt).*exp(-kappa.*(T-t)).*( (4.*rho34.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-2.*(sqrt(1-rho.^2).*rho14+rho.*rho24).*(sqrt(1-rho.^2).*rho13+rho.*rho23))./(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))./(bk)))));
q2(t)=((exp(-r02.*(T-t))).*( betaj.*(1-Kj.*Kk))).*(((-4.*aj.*varthetaj.*(1+2.*rho.*sqrt(1-rho.^2).*rho12))./(bj.^2.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2)))-( (2.*(mu-r02).*sqrt(vt).*(rho14.*sqrt(1-rho.^2)+rho.*rho24))./ (bj.*vt.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))+((sigma.*sqrt(vt).*exp(-kappa.*(T-t)).*( (4.*rho34.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-2.*(sqrt(1-rho.^2).*rho14+rho.*rho24).*(sqrt(1-rho.^2).*rho13+rho.*rho23))./(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))./(bj)))))+((exp(-r02.*(T-t)).*Kj).*( betak.*(1-Kj.*Kk))).*(((-4.*ak.*varthetak.*(1+2.*rho.*sqrt(1-rho.^2).*rho12))./(bk.^2.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2)))-( (2.*(mu-r02).*sqrt(vt).*(rho14.*sqrt(1-rho.^2)+rho.*rho24))./ (bk.*vt.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))+((sigma.*sqrt(vt).*exp(-kappa.*(T-t)).*( (4.*rho34.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-2.*(sqrt(1-rho.^2).*rho14+rho.*rho24).*(sqrt(1-rho.^2).*rho13+rho.*rho23))./(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))./(bk)))));
q3(t)=((exp(-r03.*(T-t))).*( betaj.*(1-Kj.*Kk))).*(((-4.*aj.*varthetaj.*(1+2.*rho.*sqrt(1-rho.^2).*rho12))./(bj.^2.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2)))-( (2.*(mu-r03).*sqrt(vt).*(rho14.*sqrt(1-rho.^2)+rho.*rho24))./ (bj.*vt.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))+((sigma.*sqrt(vt).*exp(-kappa.*(T-t)).*( (4.*rho34.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-2.*(sqrt(1-rho.^2).*rho14+rho.*rho24).*(sqrt(1-rho.^2).*rho13+rho.*rho23))./(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))./(bj)))))+((exp(-r03.*(T-t)).*Kj).*( betak.*(1-Kj.*Kk))).*(((-4.*ak.*varthetak.*(1+2.*rho.*sqrt(1-rho.^2).*rho12))./(bk.^2.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2)))-( (2.*(mu-r03).*sqrt(vt).*(rho14.*sqrt(1-rho.^2)+rho.*rho24))./ (bk.*vt.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))+((sigma.*sqrt(vt).*exp(-kappa.*(T-t)).*( (4.*rho34.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-2.*(sqrt(1-rho.^2).*rho14+rho.*rho24).*(sqrt(1-rho.^2).*rho13+rho.*rho23))./(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))./(bk)))));
q4(t)=((exp(-r04.*(T-t))).*( betaj.*(1-Kj.*Kk))).*(((-4.*aj.*varthetaj.*(1+2.*rho.*sqrt(1-rho.^2).*rho12))./(bj.^2.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2)))-( (2.*(mu-r04).*sqrt(vt).*(rho14.*sqrt(1-rho.^2)+rho.*rho24))./ (bj.*vt.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))+((sigma.*sqrt(vt).*exp(-kappa.*(T-t)).*( (4.*rho34.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-2.*(sqrt(1-rho.^2).*rho14+rho.*rho24).*(sqrt(1-rho.^2).*rho13+rho.*rho23))./(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))./(bj)))))+((exp(-r04.*(T-t)).*Kj).*( betak.*(1-Kj.*Kk))).*(((-4.*ak.*varthetak.*(1+2.*rho.*sqrt(1-rho.^2).*rho12))./(bk.^2.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2)))-( (2.*(mu-r04).*sqrt(vt).*(rho14.*sqrt(1-rho.^2)+rho.*rho24))./ (bk.*vt.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))+((sigma.*sqrt(vt).*exp(-kappa.*(T-t)).*( (4.*rho34.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-2.*(sqrt(1-rho.^2).*rho14+rho.*rho24).*(sqrt(1-rho.^2).*rho13+rho.*rho23))./(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))./(bk)))));
figure (1)
plot(t,q1,'r-square','LineWidth',1)
hold on
plot(t,q2,'b-x','LineWidth',1)
hold on
plot(t,q3,'g-*','LineWidth',1)
hold on
plot(t,q4,'k-o','LineWidth',1)
hold off
xlabel('t');
ylabel('q^*_j(t)')
1 Commento
Cris LaPierre
il 18 Feb 2025
Modificato: Cris LaPierre
il 18 Feb 2025
t is being interpretted as an assignment index to q. However, the first value of t is 0. This is not a valid index in MATLAB. MATLAB uses 1-based indexing.
a=2;
% Correct
a(1)
% your error
a(0)
Risposta accettata
Star Strider
il 18 Feb 2025
You need to create the ‘q’ functions as Anonymous Functions and then call them as functions of ‘t’ in the plot calls.
Try this —
mu=0.02;
betaj=0.2;
betak=0.3;
r01=0.1;
r02=0.4;
r03=0.7;
r04=1.0;
T=10;
t=0:2:20;
kappa=0.01;
sigma=1.3;
rho=0.5;
rho12=0.5;
rho13=0.5;
rho23=0.5;
rho14=0.5;
rho24=0.5;
rho34=0.5;
varthetaj=0.1;
varthetak=0.2;
aj=0.5;
ak=0.6;
bj=1.1;
bk=1.2;
vt=10;
Kj=0.5;
Kk=0.7;
q1 = @(t) ((exp(-r01.*(T-t))).*( betaj.*(1-Kj.*Kk))).*(((-4.*aj.*varthetaj.*(1+2.*rho.*sqrt(1-rho.^2).*rho12))./(bj.^2.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2)))-( (2.*(mu-r01).*sqrt(vt).*(rho14.*sqrt(1-rho.^2)+rho.*rho24))./ (bj.*vt.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))+((sigma.*sqrt(vt).*exp(-kappa.*(T-t)).*( (4.*rho34.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-2.*(sqrt(1-rho.^2).*rho14+rho.*rho24).*(sqrt(1-rho.^2).*rho13+rho.*rho23))./(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))./(bj)))))+((exp(-r01.*(T-t)).*Kj).*( betak.*(1-Kj.*Kk))).*(((-4.*ak.*varthetak.*(1+2.*rho.*sqrt(1-rho.^2).*rho12))./(bk.^2.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2)))-( (2.*(mu-r01).*sqrt(vt).*(rho14.*sqrt(1-rho.^2)+rho.*rho24))./ (bk.*vt.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))+((sigma.*sqrt(vt).*exp(-kappa.*(T-t)).*( (4.*rho34.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-2.*(sqrt(1-rho.^2).*rho14+rho.*rho24).*(sqrt(1-rho.^2).*rho13+rho.*rho23))./(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))./(bk)))));
q2 = @(t) ((exp(-r02.*(T-t))).*( betaj.*(1-Kj.*Kk))).*(((-4.*aj.*varthetaj.*(1+2.*rho.*sqrt(1-rho.^2).*rho12))./(bj.^2.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2)))-( (2.*(mu-r02).*sqrt(vt).*(rho14.*sqrt(1-rho.^2)+rho.*rho24))./ (bj.*vt.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))+((sigma.*sqrt(vt).*exp(-kappa.*(T-t)).*( (4.*rho34.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-2.*(sqrt(1-rho.^2).*rho14+rho.*rho24).*(sqrt(1-rho.^2).*rho13+rho.*rho23))./(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))./(bj)))))+((exp(-r02.*(T-t)).*Kj).*( betak.*(1-Kj.*Kk))).*(((-4.*ak.*varthetak.*(1+2.*rho.*sqrt(1-rho.^2).*rho12))./(bk.^2.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2)))-( (2.*(mu-r02).*sqrt(vt).*(rho14.*sqrt(1-rho.^2)+rho.*rho24))./ (bk.*vt.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))+((sigma.*sqrt(vt).*exp(-kappa.*(T-t)).*( (4.*rho34.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-2.*(sqrt(1-rho.^2).*rho14+rho.*rho24).*(sqrt(1-rho.^2).*rho13+rho.*rho23))./(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))./(bk)))));
q3 = @(t) ((exp(-r03.*(T-t))).*( betaj.*(1-Kj.*Kk))).*(((-4.*aj.*varthetaj.*(1+2.*rho.*sqrt(1-rho.^2).*rho12))./(bj.^2.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2)))-( (2.*(mu-r03).*sqrt(vt).*(rho14.*sqrt(1-rho.^2)+rho.*rho24))./ (bj.*vt.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))+((sigma.*sqrt(vt).*exp(-kappa.*(T-t)).*( (4.*rho34.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-2.*(sqrt(1-rho.^2).*rho14+rho.*rho24).*(sqrt(1-rho.^2).*rho13+rho.*rho23))./(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))./(bj)))))+((exp(-r03.*(T-t)).*Kj).*( betak.*(1-Kj.*Kk))).*(((-4.*ak.*varthetak.*(1+2.*rho.*sqrt(1-rho.^2).*rho12))./(bk.^2.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2)))-( (2.*(mu-r03).*sqrt(vt).*(rho14.*sqrt(1-rho.^2)+rho.*rho24))./ (bk.*vt.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))+((sigma.*sqrt(vt).*exp(-kappa.*(T-t)).*( (4.*rho34.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-2.*(sqrt(1-rho.^2).*rho14+rho.*rho24).*(sqrt(1-rho.^2).*rho13+rho.*rho23))./(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))./(bk)))));
q4 = @(t) ((exp(-r04.*(T-t))).*( betaj.*(1-Kj.*Kk))).*(((-4.*aj.*varthetaj.*(1+2.*rho.*sqrt(1-rho.^2).*rho12))./(bj.^2.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2)))-( (2.*(mu-r04).*sqrt(vt).*(rho14.*sqrt(1-rho.^2)+rho.*rho24))./ (bj.*vt.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))+((sigma.*sqrt(vt).*exp(-kappa.*(T-t)).*( (4.*rho34.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-2.*(sqrt(1-rho.^2).*rho14+rho.*rho24).*(sqrt(1-rho.^2).*rho13+rho.*rho23))./(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))./(bj)))))+((exp(-r04.*(T-t)).*Kj).*( betak.*(1-Kj.*Kk))).*(((-4.*ak.*varthetak.*(1+2.*rho.*sqrt(1-rho.^2).*rho12))./(bk.^2.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2)))-( (2.*(mu-r04).*sqrt(vt).*(rho14.*sqrt(1-rho.^2)+rho.*rho24))./ (bk.*vt.*(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))+((sigma.*sqrt(vt).*exp(-kappa.*(T-t)).*( (4.*rho34.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-2.*(sqrt(1-rho.^2).*rho14+rho.*rho24).*(sqrt(1-rho.^2).*rho13+rho.*rho23))./(4.*(1+2.*rho.*sqrt(1-rho.^2).*rho12)-(rho14.*sqrt(1-rho.^2)+rho.*rho24).^2))./(bk)))));
figure (1)
plot(t,q1(t),'r-square','LineWidth',1)
hold on
plot(t,q2(t),'b-x','LineWidth',1)
hold on
plot(t,q3(t),'g-*','LineWidth',1)
hold on
plot(t,q4(t),'k-o','LineWidth',1)
hold off
xlabel('t');
ylabel('q^*_j(t)')
.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
