how to create a vector of equations in matlab

4 Ansichten (letzte 30 Tage)
RAGHAV GOYAL
RAGHAV GOYAL am 23 Sep. 2019
Bearbeitet: John D'Errico am 23 Sep. 2019
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.

Antworten (1)

John D'Errico
John D'Errico am 23 Sep. 2019
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 Kommentare
John D'Errico
John D'Errico am 23 Sep. 2019
Bearbeitet: John D'Errico am 23 Sep. 2019
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.
John D'Errico
John D'Errico am 23 Sep. 2019
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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Systems of Nonlinear Equations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by