how to create a vector of equations in matlab

I am working on a finite horizon optimization problem
Capture.png
I want to enter these equations in Matlb for T = 0.....10
Is there a method in which I can do so? Or, do I have to enter each equation manually? My ultimate goal is to create a function of these equations which go as an input to Newton raphson method to solve these equations. Please help me.

Answers (1)

No. There will be no trivial way to magically enter those equations, and have MATLAB know how to evaluate things. In fact, it is not totally obvious to me even what you intend by some of those equations, though I might guess.
It is only a limited number of equations however. You can just bite the bullet and enter tham directly.
Better perhaps is to use a loop, in which case I fail to see where the problem lies? Note that regardless of what you do, MATLAB does not allow a zero index, so you will need to offset the index.

5 Comments

Can you tell me how to write a loop in this case? I am trying but it somehow is not working. For now I have have manually entered all the equations. But if possible, can you give me a sample look I can refer to to write a loop for my equations? Thanks.
I don't know what your equations mean, what the variables are, what are the unknowns. Nor do I know why you think you need to use Newton-Raphson. Why not just use a tool like fsolve? Writing your own solver is never a good idea, especially if you are still challenged by the language.
So, are the variables alpha, beta, delta, & sigma all known constants? Or are they unknowns? c and k appear to be vectors. Are they unknowns? Knowns?
alpha, beta, delta and sigma are constant and known. I also have the value of K0. I want to write the equations for T= 10, so I have 21 equations with 21 unknowns which I am solving using Newton method.
So write a function. But again, don't write your own optimization. If you don't even know how to create this as an objective, how can you possibly write your own optimizer with any success? Use fsolve. It might look like this:
function eqs = myobj(ck,Alpha,Beta,Delta,Sigma,K0)
% objective function for problem
% how many points? (Thus, allowing the problem
% to change size in the future.)
N = (length(ck) + 1)/2;
% since c and k will be rasied to arbitrary
% powers, allowing any elements to go negative
% will be a serious problem.
if any(ck < 0)
% post a warning, then at least resolve
% the issue in a way that will still cause
% problems, but less major problems.
warning('c or k elements are negative.')
ck = max(ck,0);
end
% unpack c and k into vectors
c = ck(1:N);
% 1st element of k is K0, fixed, and constant
k = [K0,ck(N+1:end)];
% t is an index into the vectors c and k.
t = 1:(N-1);
% set up the equations. No loop is needed at all.
eq = [realpow(c(t),-Sigma) - Beta*realpow(c(t+1),-Sigma).*(1 - Delta + Alpha*realpow(k(t+1),Alpha)), ...
c(t) + k(t+1) - realpow(k(t),Alpha) - (1-Delta)*k(t)];
end
Well, at least, that would be roughly how it looks. I've not tested the code, since I have no clue as to what values Alpha, Beta, Delta, Sigma, and Ko would be, but more importantly, because there is a huge problem with the problem you have posed.
At the end, when I needed to write out the equations, I realized that in fact, you have 20 equations, and 21 unknowns.
Essentially, your problem is underdetermined. You cannot solve such a problem, because there will be no unique solution. If any solution exists, there would be infinitely many solutions. This is the real reason I did not just make up some numbers to test out and check for any errors in the code I have written.
NOW, LET ME MAKE THIS FAR MORE CLEAR.
You have a problem with 10 instances of each equation, NOT 11. So you have 20 equations in total, NOT 21. But you have 21 unknowns.
This means your nonlinear problem is underdetermined. There will be generally be infinitely many solutions, if any solutions exist. That also means the solution you might find will be entirely dependent upon your starting values. Pick a slightly different choice of start point, and you will get an arbitrarily different result.
THIS IS A MAJOR PROBLEM WITH YOUR PROBLEM AS POSED!

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 23 Sep 2019

Edited:

on 23 Sep 2019

Community Treasure Hunt

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

Start Hunting!