Undefined function 'piecewise' for input arguments of type 'double'.

I am trying to loop through data and find the max profit. Please help!
qh=@(p) piecewise(p<=100, 50, (100<=p)&&(p<=200), 100-(p/2),p>=200, 0);
qa=@(p) piecewise(p<=150,50-(p/3),p>=150,0);
revenue=@(p) p*(qh(p)+qa(p));
cost=@(p) -2000-(20*(qh(p)+qa(p)));
z=FindMaxPrice;
function maxPro=FindMaxPrice(qh,qa)
maxPro=0;
for p=0:1:202
profit=@(p) (p - 20)*qh(p) + (p - 20)*qa(p) - 2000;
if profit(p)>maxPro
maxPro=profit(p);
end
end
maxPro
end

6 comentarios

The function piecewise is part of the symbolic toolbox: is p symbolic?
Why are you not doing this with basic numeric operations?
Yes p is symbolic. I am not doing it with basic operations because I am trying to substite p in in the function to find the max profit value. Inside my function p is defined
Unless that is not what you meant
I changed the code to look like this:
z=FindMaxPrice();
function maxPro=FindMaxPrice()
syms p
qh=piecewise(p<=100, 50, (100<=p<=200), 100-(p/2),p>=200, 0);
syms p
qa=piecewise(p<=150,50-(p/3),p>=150,0);
maxPro=0;
for p=0:202
profit=(p - 20)*qh + (p - 20)*qa - 2000;
if profit>maxPro
maxPro=profit(p);
end
end
maxPro
end
Stephen23
Stephen23 el 23 de Abr. de 2020
Editada: Stephen23 el 23 de Abr. de 2020
"Unless that is not what you meant"
If you are not sure if p is symbolic, then it probably isn't. A variable is symbolic if it is declared with sym or syms, and allows the use of the functions and methods provided in the Symbolic Toolbox:
As I wrote in my earlier comment, the function piecewise is part of that toolbox. You cannot simply use piecewise on some numeric variable that you have defined (the piecewise documentation makes it very clear that at a bare minimum its conditions must be symbolic).
"I am not doing it with basic operations because I am trying to substite p in in the function to find the max profit value"
I don't see why that requires symbolic variables: basic MATLAB numeric operations, functions, and/or indexing would probably do everything you need. You should read about logical indexing, and probably do the intrductory tutorials:
Okay! Thank you!

Iniciar sesión para comentar.

Respuestas (1)

Note that you can easily convert your piecewise symbollic expressions to appropriate numerical expressions.
So ‘qh’ and ‘qa’ become respectively:
qh=@(p) (p<=100).*50 + ((100<=p)&(p<=200)).*100-(p/2) + (p>=200).*0;
qa=@(p) (p<=150).*50-(p/3) + (p>=150).*0;
The element-wise operation (.*) isn’t always necessary. I use it in expressions such as these because it’s better to do it than not, in the event the sub-expression is a function of the argument variable (here ‘p’).
The easiest way to check to be certain these do what you want them to is to plot them:
g = linspace(0, 300, 100);
figure
plot(g, qh(g), g, qa(g))
grid
.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 23 de Abr. de 2020

Respondida:

el 23 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by