Why does my plot not display the 50 values of t_operation?

I want to plot t_operation for 100<x<150. I tried this code, but it does not work. I do not understand why.
clear all; close all; clc;
hold on
v_tank = 40;
v_trailertank = 80;
v_trailer = 100;
t_loading = 1/12;
t_unloading = 1/12;
L = 400;
for x = 100 : 150
t_tank1(x)=(t_loading+(x/v_trailertank)+t_unloading)+((L-x)/v_tank);
t_tank2(x)=(t_loading+(x/v_trailertank)+t_unloading)+x/v_trailer+(t_loading+L/(v_trailertank)+t_unloading);
t_operation(x)=max(t_tank1(x),t_tank2(x));
end
plot(x,t_operation)
Please help.

 Accepted Answer

The reason is that when you reach that plotting statement, x is simply the last value of your loop. Try this:
xrange = 100:150;
plot(xrange,t_operation(xrange))
FYI, you can write that code in vectorized fashion, without using the for loop at all:
xrange = 100 : 150;
t_tank1(x)=(t_loading+(x/v_trailertank)+t_unloading)+((L-x)/v_tank);
t_tank2(x)=(t_loading+(x/v_trailertank)+t_unloading)+x/v_trailer+(t_loading+L/(v_trailertank)+t_unloading);
t_operation(x)=max(t_tank1(x),t_tank2(x));
plot(xrange,t_operation(xrange))

5 Comments

That code makes t_tank1, t_tank2, and t_operation start with 99 0's that you don't care about. It also won't work if x has a granularity finer than 1 unit (contains non-integer values.) I would instead omit the indexing with x.
% Define constants
v_tank = 40;
v_trailertank = 80;
v_trailer = 100;
t_loading = 1/12;
t_unloading = 1/12;
L = 400;
% Define the range of x
x = 100 : 150;
t_tank1=(t_loading+(x/v_trailertank)+t_unloading)+((L-x)/v_tank);
t_tank2=(t_loading+(x/v_trailertank)+t_unloading)+x/v_trailer+...
(t_loading+L/(v_trailertank)+t_unloading);
t_operation=max(t_tank1,t_tank2);
% Plot all three lines
plot(x, t_tank1, 'r-', 'DisplayName', 'tank 1');
hold on
plot(x, t_tank2, 'c-', 'DisplayName', 'tank 2');
plot(x, t_operation, 'k:', 'LineWidth', 4, 'DisplayName', 'maximum')
legend show
I chose to plot all three lines in different colors and with the operation variable with a thicker line for illustration purposes.
If I wanted to rerun that model with a finer-grained x, the only line that needs to change is the line defining x.
x = 100:0.5:150;
Rerun the rest of the lines from "t_tank1 = ..." as before.
Now I want to know the value of ‘x’ when t_operation is at a miniumum. I changed the code for more accuracy. I know the minium of t_operation is 8.4405, but I do not know to what ‘x’ this corresponds.
The code now:
clear all; close all; clc;
hold on
v_tank = 40000;
v_trailertank = 80000;
v_trailer = 100000;
t_loading = 1/12;
t_unloading = 1/12;
L = 400000;
% Opdracht 1a: plot t_operation(x) voor 100 =< x =< 150
x = 100000:150000;
t_tank1(x)=(t_loading+(x/v_trailertank)+t_unloading)+((L-x)/v_tank);
t_tank2(x)=(t_loading+(x/v_trailertank)+t_unloading)+x/v_trailer+(t_loading+L/(v_trailertank)+t_unloading);
t_operation(x)=max(t_tank1(x),t_tank2(x));
plot(x,t_operation(x))
%geef minimum
t_operationmin = min(t_operation(x))
%now: to what ‘x’ corresponds t_operationmin?
Never mind: I already found it out. It works with: X_min=find(t_operation(x)==t_operationmin)
You can do the same thing more efficiently with
[t_operationmin, x_min_index] = min(t_operation(x))
But be careful. The find command will tell you which element (e.g. the "5th element in the vector"), not the actual value of x that that corresponds to. I think you'll need x(x_min_index) for that. For example, in your case, the 5th element of x would be the value x=100004.
As Steven pointed out, you are using x in an odd way, as both an index and a variable. So you need to be careful.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!