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

3 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

John Knollmeyer
John Knollmeyer il 26 Lug 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)

Più risposte (0)

Categorie

Scopri di più su Mathematics in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by