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

% 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 commentaires

Which part exactly do you want to turn into a function?
fminbind is already a function and you can't turn it into more of one - unless you are using a class you have to output the result from it and assign it to a variable.
@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.

Connectez-vous pour commenter.

Réponses (1)

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 commentaires

I tried this, but I am getting the error: "Function definitions are not permitted in this context." when I do this.
Matt J
Matt J le 27 Oct 2016
Modifié(e) : Matt J le 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.

Connectez-vous pour commenter.

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by