# Writing a long equation in MATLAB

7 views (last 30 days)
Selim Elbadri on 25 Aug 2022
Commented: Voss on 28 Aug 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 CommentShowHide None
David Hill on 25 Aug 2022
Show us your code and ask a specific question. You should also describe or attach your variables and constants.

Voss on 26 Aug 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.
Voss on 28 Aug 2022
You're welcome!

### Categories

Find more on Gamma Functions in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!