getting min & max of a function for different y's

3 ビュー (過去 30 日間)
soloby
soloby 2015 年 6 月 23 日
コメント済み: Guillaume 2015 年 6 月 26 日
If I were to have
x = -10:0.1:10;
y = 0:0.005:1;
f1 = trapmf(x,[-2 0 0 2])
is there anyway I can get min(x) and max(x) for all y's?
like
[min(x), max(x)] of f1 for y = 0
[min(x), max(x)] for y = 0.005
...
[min(x), max(x)] for y = 1
I've been wondering if it'd be any easier for me to play with the inverse of this function?
  5 件のコメント
soloby
soloby 2015 年 6 月 23 日
編集済み: soloby 2015 年 6 月 23 日
that's a good point. I think i'm on the wrong approach with this, I need equal increments of f (y-axis) and its corresponding x-values, not equal increments of x and the corresponding f values of it, which is what I've done so far.
This way I won't runinto problems for those results which have no f values
soloby
soloby 2015 年 6 月 23 日
I'm thinking just something simple like
x = -10:0.1:10;
f1 = trapmf(x,[-2 0 0 2]);
plot(f1,x)
for k = 1:length(f1)
minNmax1(k,:) = [min(f1),max(f1)];
end
but this will give me a 201 points of 0 and 1 so it's not taking consideration into the iterations.
if i could just do that.. it'll be a piece of pie afterwards

サインインしてコメントする。

回答 (3 件)

Guillaume
Guillaume 2015 年 6 月 23 日
A simple way would be to use a for loop over the values of y. Within the loop, you'd find the first and last index of f where it is equal to y. Use these two indices to return the corresponding value of x. That's the min and max.
xmin = nan(size(y));
xmax = nan(size(y));
for yiter = 1:numel(y)
indices = find(f == y(yiter));
if ~isempty(indices)
xmin(yiter) = x(indices(1));
xmax(yiter) = x(indices(end));
end
end
The above returns nan for the value of y for which f has no value.
  2 件のコメント
soloby
soloby 2015 年 6 月 23 日
I'm getting all NaN's for xmax and xmin except the very first and the last?
I shouldn't have any NaN's, isn't that correct?
f1 is a triangular wave so it does not have any voids in the function?
Guillaume
Guillaume 2015 年 6 月 26 日
You should have NaNs for any y value for which there is no f value. I expect there are plenty of them. I don't have the Fuzzy Logic Toolbox so I can't check how it generates your f, but another reason why you'd have some NaN is that a 0.35 in your f may not be exactly the same 0.35 as in your y. One might be 0.350000000...00001 and the other 0.34999...999999999 which matlab will display the same.
To fix this, you can change the find condition to
indices = find(abs(f-y(yiter)) < 1e-10); %or whatever tolerance you want

サインインしてコメントする。


Katalin
Katalin 2015 年 6 月 24 日
編集済み: Katalin 2015 年 6 月 24 日
Try this:
x = -10:0.1:10;
for i=1:200
y= 0.005*i;
f1 = @(x) trapmf(x,[-2 0 0 2])-y;
for ij = -10:0.01:10
ab = fzero(f1,ij);
in = int8((10.01+ij)*100);
b(in) = ab;
end
result = unique(b)
end
  2 件のコメント
soloby
soloby 2015 年 6 月 25 日
doesn't seem to work. i am getting errors for ~10 minutes before the coding stops
Katalin
Katalin 2015 年 6 月 25 日
what is the error?

サインインしてコメントする。


Jan
Jan 2015 年 6 月 24 日
There are no voids in f1, but you cannot assume, that there are values, which are exactly 0.01 etc. Remember that the value of f1 might be 0.0099999999999999 or any other value, which is very near to 0.01.
If you leave the idea of searching for exact identify, but search for intervals, the problem is solved directly by histc or histcounts (depending on the Matlab version you are using).

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by