How to plot a graph for variable cost per min of a product
조회 수: 1 (최근 30일)
이전 댓글 표시
I want to calculate total cost of a product on the bases of time as shown in the following script however I am unable to show on graph. I want to add second part of the curve with the 1st part. Thank you for helping me. and is there any command to fetch value from graph?
a = 10; %cost per min for 1st 3 mins b = 5; % cost per min for last 2 mins
x = 0:1:5; % time (5 mins) y = a*(x.*(x<=3)) + b*(x.*(x>3));
plot (x,y);
Total_Cost = a*3 + b*2 %40
댓글 수: 0
채택된 답변
John Knollmeyer
2016년 7월 26일
"I want to add the second part of the curve to the first part"
You could break your piecewise function into two separate functions and then concatenate them
first_component = a * (0:3);
second_component = b * (1:2) + first_component(4);
y = [first_component, second_component];
"Is there any command to fetch value from the graph?"
If you save the function handle like so
figure_handle = plot(x,y);
then you can access the y values from it
Total_Cost_from_graph = figure_handle.YData(end)
Integral Tool
You didn't ask this in the question, but the integral tool is useful for problems like this
% y = 10, for 0 <= y <= 3
function_a = @(x) 10 * ones(size(x));
% y = 5, for 4 <= y <= 5
function_b = @(x) 5 * ones(size(x));
Total_Cost_Using_Integral = integral(function_1, 0, 3) + integral(function_2, 3, 5)
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!