Writing a long equation in MATLAB

7 views (last 30 days)
Selim Elbadri
Selim Elbadri on 25 Aug 2022
Commented: Voss on 28 Aug 2022
I need help writing this equation in MATLAB:
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?
  1 Comment
David Hill
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.

Sign in to comment.

Accepted Answer

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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!