How to make a function that returns the difference between the passed function's max and min value given a range of the independent variable?

5 次查看(过去 30 天)
I was given the task to develop a function M-file that returns the difference between the passed function's maximum and minimum value given a range of the independent variable. In addition, I have to make the function generate a plot of the function for range. I was given three instances to test my M-file.
a.) f(t) = 8e^(-0.25t)sin(t - 2) , from t=0 to 6pi
b.) f(x) = e^(4x)sin(1/x) , from x =0.01 to 0.02
c.) The built in humps function from x = 0 to 2
I found the exact problem on Chegg, but it provides absolutely no explanation of the code and I would really like to be able to understand this problem. I hope someone can help!
  4 个评论
NikePro
NikePro 2016-2-10
So far my friend and I have gotten this;
function ht = func(f,a,b,int,varagin)
x = linspace(a,b,int);
y = f(x,varagin{:});
ht = max(y) - min(y);
fplot(f,[a,b],varagin{:})
Here f would = my function, a would be lower limit, b is my upper and int would be my number of intervals. But, I keep getting an error with:
x = linspace(a,b,int);
I am not sure what my error would be
Image Analyst
Image Analyst 2016-2-10
We're not either since I didn't run it and you forgot to post the error. It looks a lot more complicated than my solution below though.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2016-2-9
Hint: You need to use * for when you multiply, and use exp() for e^, and you can use linspace() to get a bunch of values for t between some starting and stopping values.
t=linspace(0,6*pi, 1000); % Get 1000 values of t
f = 8 * exp(-0.25*t).*sin(t - 2);
plot(t, f);
grid on;
I'm assuming you know how to use min() and max() and how to subtract the values they return.
  2 个评论
Image Analyst
Image Analyst 2016-2-10
I though you knew how to use min and max also. To use the min and max functions, you do it like this:
minValue = min(f);
maxValue = max(f);
message = sprintf('The min value = %f\nThe max value = %f', minValue, maxValue);
uiwait(helpdlg(message));
To do an entire function you'd have this:
function [minValue, maxValue] = FindMinAndMax(t1, t2)
t=linspace(t1, t2, 1000); % Get 1000 values of t
f = 8 * exp(-0.25*t).*sin(t - 2);
plot(t, f);
grid on;
minValue = min(f);
maxValue = max(f);
message = sprintf('The min value = %f\nThe max value = %f', minValue, maxValue);
uiwait(helpdlg(message));
To call it, you'd do
[minValue, maxValue] = FindMinAndMax(0, 6*pi)
Does that explain it enough? Repeat for all the other functions.

请先登录,再进行评论。

更多回答(2 个)

Matt J
Matt J 2016-2-9
编辑:Matt J 2016-2-9
Hint: fminbnd and fplot could be useful commands here.
  3 个评论
Matt J
Matt J 2016-2-10
编辑:Matt J 2016-2-10
Probably because fminbnd is finding local minima. To avoid this, you would do a sample search, similar to what ImageAnalyst proposes with linspace, to get a tighter interval around the global solution.
dpb
dpb 2016-2-10
编辑:dpb 2016-2-11
"...because fminbnd is finding local minima..."
Yes, precisely. fminsearch, otoh, does fine on this one (again, didn't try the rest).
ADDENDUM OTOH, given that one can give a full range as input to fminbnd, one would hope it were more robust than it appears to be; this isn't a very difficult function and to get stuck way over to the right just seems wrong behavior. END ADDENDUM
I simply mentioned it so the OP wouldn't (hopefully) just "plug 'n play" and find other than happiness on proud submittal.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2016-2-10
There is only one possible strategy for the question as phrased: you need to test every representable number between the two bounds. If you test 343.93244934231250908851507119834423065185546875 but not 343.93244934231250908851507119834423065185546875 + 5.684341886080801486968994140625e-14 then you might miss the global minima or global maxima, because the function might be (for example)
f = @(x) x - realmax .* (x == 343.932449342312565931933932006359100341796875);
You need to test ever single representable number -- unless, that is, there are some particular guarantees on the form of the function, such as it being a polynomial.

Community Treasure Hunt

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

Start Hunting!

Translated by