How could I make my cost variable into a function so that I could calculate the minimum cost?

3 ビュー (過去 30 日間)
SnoopingPoppet
SnoopingPoppet 2016 年 10 月 27 日
コメント済み: Rena Berman 2017 年 9 月 28 日
% This calculates minimum cost of a material using fminbnd or fminsearch, both of which I need to turn cost into either an anonymous or custom function, but I'm not sure how.
surfaceArea = (2*length*width) + (2*length.*thickness)
+ (2.*thickness.*width); %In units of feet^2
volume = width.*thickness.*length;
%In units of feet^3
fixedCost = 3000;
%fixedCost, paintGalv, materialCost, thickDisc, and cost are in units of $$
paint = 10.*surfaceArea;
materialCost = 50.*volume;
thickDisc = 5000.*thickness;
cost = fixedCost + paint + materialCost - thickDisc;
%%%How can I make this into a FUNCTION ^^^???
minCost = fminbnd(cost,1,200);
  3 件のコメント
Stephen23
Stephen23 2017 年 9 月 21 日
@Snooping Poppet: it is not appreciated when you edit away the text of your question. Imagine if everyone did that, then this entire forum would be useless for anyone else as it would only consist of thousands of threads all reading "This post was taken down". Would you find that useful?
By deleting your question you unilaterally decided that Matt J's volunteered time helping you shall not be useful to anyone else. Does Matt J get a say in this decision? Do you think this is why we volunteers come here and write our advice and comments?
If you want a consulting service then you will find plenty of them on them internet. This is a community forum with answers provided by volunteers.
Rena Berman
Rena Berman 2017 年 9 月 28 日
(Answers Dev) Restored edit

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

回答 (1 件)

Matt J
Matt J 2016 年 10 月 27 日
編集済み: Matt J 2016 年 10 月 27 日
fminbnd is for 1D function minimization. You cannot use it if you have 3 unknowns (length, width, and thickness). However you can use fminsearch
c=fminsearch(@costfunc,p0)
with an appropriate initial guess of p0=[length,width,thickness] and with costfunc() defined as,
function cost=costfunc(params)
length=params(1);
width=params(2);
thickness=params(3);
surfaceArea = (2*length*width) + (2*length.*thickness)
+ (2.*thickness.*width); %In units of feet^2
volume = width.*thickness.*length;
%In units of feet^3
fixedCost = 3000;
%fixedCost, paintGalv, materialCost, thickDisc, and cost are in units of $$
paint = 10.*surfaceArea;
materialCost = 50.*volume;
thickDisc = 5000.*thickness;
cost = fixedCost + paint + materialCost - thickDisc;
end
  3 件のコメント
Matt J
Matt J 2016 年 10 月 27 日
編集済み: Matt J 2016 年 10 月 27 日
You cannot define a function at the command line. Furthermore, prior to MATLAB version R2016b, you cannot define costfunc in a script. It must live either in its own .m file, or it must be a local function residing in another mfunction's .m file.

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

カテゴリ

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

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by