How could I make my cost variable into a function so that I could calculate the minimum cost?
1 次查看(过去 30 天)
显示 更早的评论
% 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
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.
回答(1 个)
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
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!