How to plot a graph for variable cost per min of a product

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

John Knollmeyer
John Knollmeyer am 26 Jul. 2016
"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)

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by