How to obtain argument value of an equation

조회 수: 3 (최근 30일)
moh mor
moh mor 2023년 4월 12일
댓글: moh mor 2023년 4월 12일
Hello, I want to know for what values of x, f(x) is equal to half of its max value. I want to use value of this argument in my future computation in MATLAB. How can I do this in MATLAB?
  댓글 수: 2
Sam Chak
Sam Chak 2023년 4월 12일
Can you define the meaning of "half of its max value"?
x = linspace(0, 10, 1001);
f = 1*sin(pi/5*x) - 1*tanh(2.3*(x - 5)) + 8;
plot(x, f, 'linewidth', 1.5), grid on, ylim([0 12])
halfmax = max(f)/2
halfmax = 5.0000
yline(max(f), '--', 'max');
yline(halfmax, '--', 'halfmax');
moh mor
moh mor 2023년 4월 12일
Ok. You are right. I I just mean how to obtain value of x in for example f(x)=constant .

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2023년 4월 12일
편집: John D'Errico 2023년 4월 12일
I wrote an allcrossings tool, that I posted on the file exchange. I've attached it here.
help allcrossings
allcrossings: locate all intersections of a pair of functions f1 and f2, on a finite domain usage: Xcross = allcrossings(f1,f2,Xsupport,nsamples) allcrossings uses fzero as a search engine for the roots of f1(x) - f2(x) == 0, once it identifies a bracket that contains a root. If your functions have discontinuities in them, such as tan(x), then an identified crossing may be spurious, in the sense that f1(x)~=f2(x), but the two functions should still exhibit a crossing at that location. arguments: (input) f1,f2 - a pair of function handles. It is assumed that f1 and f2 are both vectorized functions, so they can be evaluated at multiple locations in one call. if you are trying to solve problems of the form f1(x) == k, then f2 can be passed in as a scalar constant, or you can use f2 = @(x) k + zeros(size(x)); as a properly vectorized function. Xsupport - vector of length 2 that defines the lower and upper limits of the domain where the search will be done. nsamples - (optional) integer argument that indicates the number of points over that domain to test, looking for where there may be an intersection. At a minimum, nsamples will always be at least 100. Default value: nsamples = 500; fzeroOptions - (optional) if supplied, it must abe a valid struct containing the options used by fzero. The options that fzero will look for are: {Display, TolX, FunValCheck, OutputFcn, and PlotFcns}. The values assumed will otherwise be: Display: 'none' TolX: 2.2204e-16 FunValCheck: 'off' OutputFcn: [] PlotFcns: [] arguments: (output) Xcross - vector of locations where f1(x) == f2(x) Examples: Intersection points of sin(x) == cos(x), between -10 and +10. f1 = @(x) sin(x); f2 = @(x) cos(x); xcross = allcrossings(f1,f2,[-10,10],50) xcross = -8.6394 -5.4978 -2.3562 0.7854 3.927 7.0686 Example: Positive solutions to the problem x + tan(x) == 1 f1 = @(x) 2*x + tan(x); f2 = 1; xcross = allcrossings(f1,f2,[0,20],100) xcross = 0.32919 1.5708 1.9113 7.854 7.9213 14.137 14.174 f1(xcross) ans = 1 1.3748e+15 1 -2.4185e+14 1 -2.0929e+14 1 As you can see, there are crossings found at certain points that are not technically solutions, but due to the jump in the tan function, they were still identified by fzero as "crossings". These are spurious solutions. see also: fzero, fzolve, solve, vpasolve author: John D'Errico e-mail: eoodchips@rochester.rr.com Date: 2/28/2023
For example, consider the function
fun = @(x) abs(x).^1.5./(1 + (x-1.23).^2);
fplot(fun,[-10,10])
grid on
hold on
Assume we are willing to search over the interval in x of [-10,10]. (I know this function approaches zero asymptotically as x goes to infinity in either direction. So I need not worry about any other peaks.)
First, find the peak value.
[xmax,fmax] = fminbnd(@(x) -fun(x),-10,10);
xmax
xmax = 1.7786
fmax = -fmax
fmax = 1.8233
So the peak value lies around 1.7786, and the peak itself is 1.823...
Now we find the two points where the function attains the value at half the peak.
xcross = allcrossings(fun,fmax/2,[-10,10],1000)
xcross = 1×2
0.9792 4.0449
xline(xcross,'r')
yline(fmax/2,'g')
So the two points where the function attains half the peak height.

추가 답변 (1개)

Vilém Frynta
Vilém Frynta 2023년 4월 12일
My approach:
f = @(x) x^2; % function example
[max_val, max_idx] = fminbnd(@(x) -f(x), -10, 10); % find maximum value of the f
half_max_val = max_val/2; % divide maximum by two
  댓글 수: 1
John D'Errico
John D'Errico 2023년 4월 12일
편집: John D'Errico 2023년 4월 12일
NO. You misunderstood the question. The question was NOT to compute the maximum value, and then divide by 2.
The question was to locate the point x, where the function attains half that maximum value. While you did learn the maximum, you did not solve the real problem.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by