Writing a long equation in MATLAB

6 visualizzazioni (ultimi 30 giorni)
Selim Elbadri
Selim Elbadri il 25 Ago 2022
Commentato: Voss il 28 Ago 2022
Hi,
I need help writing this equation in MATLAB:
where:
My proposed code is:
d = ((1/(gamma*(abar(t-1,1) - a(t-1,1))))*(1+(a(t-1,1)/(gamma*(abar(t-1,1) - a(t-1,1))))));
GAMMA = gamma*(abar(t-1,1) - a(t-1,1);
exp(-rho*t)*gamma*(abar(t-1,1) - a(t-1,1))*((exp(-(rho+d)*t)/c0)-(exp(-rho*t)/(GAMMA*d))))^(-1);
The code doesn't generate the right numbers though. Is it written correctly?
Thanks!
  1 Commento
David Hill
David Hill il 25 Ago 2022
Show us your code and ask a specific question. You should also describe or attach your variables and constants.

Accedi per commentare.

Risposta accettata

Voss
Voss il 26 Ago 2022
There are a couple of errors with the parentheses:
d = ((1/(gamma*(abar(t-1,1) - a(t-1,1))))*(1+(a(t-1,1)/(gamma*(abar(t-1,1) - a(t-1,1))))));
GAMMA = gamma*(abar(t-1,1) - a(t-1,1));
% ^ you were missing this parenthesis
c = exp(-rho*t)*gamma*(abar(t-1,1) - a(t-1,1))*((exp(-(rho+d)*t)/c0)-(exp(-rho*t)/(GAMMA*d)))^(-1);
% ^ and you had an extra parenthesis in here
In addition to that, there are some extraneous parentheses that won't affect the result. Removing those, the code looks like this:
d = 1/(gamma*(abar(t-1,1) - a(t-1,1)))*(1+(a(t-1,1)/(gamma*(abar(t-1,1) - a(t-1,1)))));
GAMMA = gamma*(abar(t-1,1) - a(t-1,1));
c = exp(-rho*t)*gamma*(abar(t-1,1) - a(t-1,1))*((exp(-(rho+d)*t)/c0)-exp(-rho*t)/(GAMMA*d))^-1;
In addition to that, you can calculate GAMMA first and use it in the other expressions to shorten them a bit:
GAMMA = gamma*(abar(t-1,1) - a(t-1,1));
d = 1/GAMMA*(1+a(t-1,1)/GAMMA);
c = exp(-rho*t)*GAMMA*((exp(-(rho+d)*t)/c0)-exp(-rho*t)/(GAMMA*d))^-1;
Throughout, I've assigned your last expression to the variable c.
  3 Commenti
Selim Elbadri
Selim Elbadri il 27 Ago 2022
Thanks a lot!
Voss
Voss il 28 Ago 2022
You're welcome!

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by