Dynamic optimization with three functions, where two of them are subfunctions.

2 ビュー (過去 30 日間)
Amit Kumar
Amit Kumar 2018 年 10 月 28 日
編集済み: Amit Kumar 2018 年 10 月 29 日
Hello,
I am currently working on a excercise sheet on Matlab-functions, and I am having difficulties to code a function the way it is supposed to work. Basically I have 3 functions, where 2 of them are subfunctions.
The first function:
function c = nutzen(S,x,W)
c = 7 * min(S+x,W) - 2*x-3*(x>0)-1*max(0,S+x-W)
end
The second function:
function Sneu = transformationsfunktion(S,x,W)
Sneu = max(0,S+x-W)
end
Then, both of them need to be incorporated into the third:
function V = wertfunktion(S,t)
if t==0
V = 0
end
W = 3
for x = 0:10
v(x) = nutzen(S,x,W) + wertfunktion(transformationsfunktion(S,x,W),t-1)
end
V = max(v(x))
end
To start this code, I have to give values for S and t, but afterwards the code gets into an endless loop...
Actually, it should for x=0:10 calculate the value for v(x) and then after 11 loops give me the max value. Seems to be quite simple.
Does anyone see where my mistake is?
Any help is much appreciated.
  11 件のコメント
Torsten
Torsten 2018 年 10 月 29 日
編集済み: Torsten 2018 年 10 月 29 日
As written, v is an array, and you assign a value to array element number x of v.
Note again that arrays start with array element 1, not 0.
Amit Kumar
Amit Kumar 2018 年 10 月 29 日
編集済み: Amit Kumar 2018 年 10 月 29 日
Ok, this I understand now as well. There is no '0th' element in a row. But my exercise sheet asks me to calculate v(x) starting with x=0.
My current code:
function V = wertfunktion(S,t)
if t==0
V = 0
end
W = 3
for x = 1:10
v(x) = nutzen(S,x,W) + wertfunktion(transformationsfunktion(S,x,W),t-1)
end
q = nutzen(S,0,W) + wertfunktion(transformationsfunktion(S,0,W),t-1)
v = [q v]
V = max(v)
end
Now I have to stop this endless loop.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeSurrogate Optimization についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by