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

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 2020년 4월 23일
편집: Stephen23 2020년 4월 23일
"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!

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2020년 4월 23일
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
.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 4월 23일

답변:

2020년 4월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by