how to plot a bounding min/max function around the output of a Monte Carlo simulation?

2 次查看(过去 30 天)
Consider the following Matlab code that generates and plots 3 random functions that are sampled at different timestamps within the time period [0,1]. Note that the timestamps for each sample are different.
if true
% code
end % code
t=rand(30,3);
st=zeros(30,3);
for k=1:3
st(:,k)=sort(t(:,k));
end
d=2-2*rand(30,3);
figure; plot(st(:,1),d(:,1)); hold on;
for k=2:3
plot(st(:,k),d(:,k));
end
grid minor;
I want to calculate and plot the minimum and the maximum functions.
Please note
min(d')' does not find the minimum at all the timestamps. Any idea please?
  1 个评论
Image Analyst
Image Analyst 2016-5-14
Do you want the min value of the data for each time, st, value? This would just be the data sorted since there is just one value at each time stamp. OR do you want the data going along the very bottom-most line that gets plotted?

请先登录,再进行评论。

回答(2 个)

Mohamed Eljali
Mohamed Eljali 2016-5-14
编辑:Mohamed Eljali 2016-5-14
% code
clc;
clear all;
close all;
t=rand(30,3);
st=zeros(30,3);
for k=1:3
st(:,k)=sort(t(:,k));
end
d=2-2*rand(30,3);
figure; plot(st(:,1),d(:,1)); hold on;
for k=2:3
plot(st(:,k),d(:,k));
end
hold on;
grid minor;
%Let us assume minimum=inf and their location= (0,0)
minimum_value=Inf;
i1=0;
j1=0;
%Let us assume maximum=-inf and their location= (0,0)
maximum_value=-Inf;
i2=0;
j2=0;
for k=2:3
for i=1:30
% Finding or updating mimimum value and their location
if minimum_value> d(i,k)
minimum_value=d(i,k);
i1=i;
j1=k;
end
% Finding or updating maximum value and their location
if maximum_value<d(i,k)
maximum_value=d(i,k);
i2=i;
j2=k;
end
end
end
%Finally plotting minimum and maximum values in the graph
plot(st(i1,j1),d(i1,j1),'*r','Linewidth',3); hold on
plot(st(i2,j2),d(i2,j2),'*k','Linewidth',3);
  1 个评论
Image Analyst
Image Analyst 2016-5-15
If all he wanted was the global min and global max, he would simply use min() and max() like this:
[minDValue, index] = min(d(:))
stAtMinD = st(index)
[maxDValue, index] = max(d(:))
stAtMaxD = st(index)
It's simpler than the for loops because it calculates the min and location for you - you don't have to do it yourself in a for loop.
I interpreted this "min(d')' does not find the minimum at all the timestamps" to indicate that he wanted the min for all times, not just the global min. Like he wants the lowest line going across the timeline, not just one single point. That's a lot tougher to get - he'll have to increase resolution to get the lines everywhere. If he doesn't want it everywhere but only at the discrete locations of st, then that's trivial - just sort st and d and you're done. Since the timestamps are unique, there is only one value for each timestamp, and so that value is both the max and the min.

请先登录,再进行评论。


Mohamed Eljali
Mohamed Eljali 2016-5-14

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by