Why does my plot not display the 50 values of t_operation?
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
the cyclist
2019년 9월 27일
편집: the cyclist
2019년 9월 27일
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
the cyclist
2019년 9월 27일
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.
추가 답변 (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!