Need to find the local maxima and minima of a cubic function, then write an iterating function.

3 次查看(过去 30 天)
Hi all,
I have a defined function P(V) = @(V) that takes the cubic form. From 0 to the local maximum the function has three real roots.
I want to write an if/else statement using these building blocks:
vpasolve(isotherm1fxn(V) == 10.1, [0 inf])
j = 0.001;
for j = 0.001:10.1
rootstorage = zeros(3, 1)
rootstorage(1:3, 1) = vpasolve(isotherm1fxn(V) == j, V, [0 inf])
int1 = abs(integral(isotherm1fxn, rootstorage(1, 1), rootstorage(2, 1)))
int2 = abs(integral(isotherm1fxn, rootstorage(2, 1), rootstorage(3, 1)))
sumInts = int2-int1
if abs(sumInts) <= 0.001
return;
else j = j+.001
end
end
The if/else loop should iterate over the function in intervals of 0.01. j is the dummy value here. I want the function to solve for the three roots of the function for the given value of j, then integrate over the function from the first root to the second root (negative area), then integrate over the function from the second root to the third root (positive area). If the difference betweeen the first and second integral is less than the tolerance, in this case 0.001, I want the statement to stop and return the value of j at which that occurs. If the difference is greater than the tolerance, I want to increase j by .001 and repeat the process. This code should continue until either it finds a j for which the integral difference condition is satisfied, or j gets above the maximum value without ever having satisfied the process, at which the loop should return NaN.
I've run this code and it doesn't seem to work- the function is returning multiple values of j and sumInts that don't satisfy the conditions. Could anyone help?

回答(1 个)

Walter Roberson
Walter Roberson 2019-11-19
编辑:Walter Roberson 2019-11-19
for j = 0.001:10.1
j increments by 1
else j = j+.001
You are trying to modify the loop control variable. That is legal, but it does not alter the flow of control. MATLAB records the values of the loop control variable internally and uses those. The flow is like
_internal_lower_bound = 0.001;
_internal_upper_bound = 10.1;
_internal_increment = 1;
if _internal_lower_bound > _internal_upper_bound
j = [];
else
_internal_variable = _internal_lower_bound;
while _internal_variable <= _internal_upper_bound
j = _internal_variable;
body of the for goes here and can modify j
_internal_variable = _internal_variable + _internal_increment;
end
end
If you examine this flow of control, then you can see that the loop variable, j can be modified, but that except in the case where there are no more iterations the value of j is set back to what it would be if it had not been modified.
If you want j to increment by 0.001 then you code that in the for loop:
for j = 0.001:0.001:10.1
and do not modify j inside the for loop.
  2 个评论
cmcelm
cmcelm 2019-11-19
I still don't understand how I can write a loop that will return only the values of j for which abs(sumInts) <= 0.
Walter Roberson
Walter Roberson 2019-11-19
function j = Find_j
j = 0.001;
for j = 0.001: 0.001 : 10.1
rootstorage = zeros(3, 1)
rootstorage(1:3, 1) = vpasolve(isotherm1fxn(V) == j, V, [0 inf])
int1 = abs(integral(isotherm1fxn, rootstorage(1, 1), rootstorage(2, 1)))
int2 = abs(integral(isotherm1fxn, rootstorage(2, 1), rootstorage(3, 1)))
sumInts = int2-int1
if abs(sumInts) <= 0.001
return
end
end
j = []; %signal there was no value
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by