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

3 views (last 30 days)
% 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 Comments
Stephen23
Stephen23 on 21 Sep 2017
@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.

Sign in to comment.

Answers (1)

Matt J
Matt J on 27 Oct 2016
Edited: Matt J on 27 Oct 2016
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 Comments
Matt J
Matt J on 27 Oct 2016
Edited: Matt J on 27 Oct 2016
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.

Sign in to comment.

Categories

Find more on Simscape in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!