I would like to solve the following optimization problem with Matlab: minimize f(t) when t >= 0.
There's fseminf function but i didn't understand well how to apply it to my case. Also it seems a bit overkill to use such a powerful tool for such a seemingly easy problem. I'll be grateful for any tips on how to apply fseminf here and any suggestions how else it can be solved.
Matlab is a numerical software so one 'simple' way to solve this problem is to calculate the value of f(t) for values of t>0 and then find the minimum of that. Depending on the function the number of ts that you want to evaluate might be smaller or larger.
One possible solution could be:
t = 0:0.001:10; % create values from 0 to 10 in steps of 0.001
f = t.^3+5; % evaluate the function for each value of 't'
[minF, locT] = min(f);
minF % this is the smallest value of the function
t(locT) % the minimum value occurred at this 't'
You should define t to be in the region where you expect the minimum to, if you define it wrong this would only find the local minimum. If the spacing between separate 't's is too large the minimum might also fall between them, that is why I choose a relatively small step of '0.001'.
Related
I am trying to solve two coupled algebraic equation
f1(x,y) = 0;
f2(x,y) = 0;
typical order of magnitude of the functions f1 and f2 are 10^42 . I ran the matlab code but it said no solution found. I figured that the problem is because scales involved is very high. Rescaling the whole equation is pretty tedious. I want to stop the root finding function (fsolve) when delta(f)/f < epsilon(say 1e-6) . How can this condition implemented in matlab? Any alternative solution to the scaling problem is also welcome.
RTFM (friendly of course), https://de.mathworks.com/help/optim/ug/fsolve.html
The options that you can provide to the solver contain the parameter TolFun with default value 1e-6 that is the absolute tolerance for the function value. Apparently there is no provision for relative tolerance, so you need to compute the function value scale from the initial point or more global considerations to set TolFun = scale * epsilon.
Firstly, I'm sure a simple answer exists for this, maybe I'm just not wording it right in searching for an answer online.
I'm trying to solve an equation that looks like this:
a*x*cot(a*x) == b
Where a and b are constants. Using
solve(a*x*cot(a*x) == b, x)
I'm getting a result I know is wrong (with the values I'm using for the constants, I'm getting like -227, and it should be something around +160.) I plotted it up in Mathematica as two separate functions, and they do cross each other right around there, but since the cot part is periodic, they do so many times.
I want to constrain Matlab's search for the solution to a specific interval, such as 0 to 200; how do I do that?
I'm pretty new to Matlab (rather more experienced in Mathematica).
You can specify the bounds on x using fzero with only two requirements
The function must be in a "residual" form (i.e., r(x) = 0)
The residual values at the two bounds must have opposite sign (this guarantees that a root exists within the interval for continuous functions).
So we re-write the function in residual form:
r = #(x) a*x*cot(a*x) - b;
define the interval
% These are just random numbers; the actual bounds should come
% from the graph the ensures r has different signs a xL and xR
xL = 150;
xR = 170;
and solve
x = fzero(r,[xL,xR]);
I see you were trying to use the Symbolic Toolbox for a solution, but since the equation is a non-linear combination of a polynomial and a trigonometric function, there is more than likely no closed form solution. So I differed to a non-linear, numeric root-finder.
I tried some values and it seems solve returns a numeric solution. This is the documented behaviour if no analytic solution is found.
In this case, you may directly call the numeric solver with a matching start value
vpasolve(a*x*cot(a*x) == b, x,160)
It's not exactly what you asked for, but using your reading from the plot as a start value should do it.
Consider 2 sets
A = randi(1000,100,7);
B = randi(700,300,7);
I would like to find a function : B# = optimf(A,B) and gives me B# = {100x7} which is a collection of rows from B such that some attribute( eg. mean ) is minimum.
For eg: B# = optimf(A,B) such that mean(B#) - mean(A) is minimum.
Any ideas?
According to me Optimization is finding the best suitable value of a function. For example, if you have an equation and you need to find minimum value at which the equation satisfies criteria, then this is an optimization problem.
There is no such optimization function AFAIK for your function. But, you can take help of optimization algorithms like Least Square Errors
Or simply use some filter of MATLAB.
I hope it helps.
UPDATE
(Not a sophisticated solution but just works in some cases.)
Step 1-
Make a for-loop and select randomly some values from input vector. So you get a random subset.
Step 2
Define a cost function. A function that can measure how good is the subset. The function will take input as vector and gives output a numerical quantity such as % of quality.
Step 3
Go on taking these readings. Take the max value of output function and its corresponding vector. That should be a solution.
OR
use algorithms like ACO
I have a curve IxV. I also have an equation that I want to fit in this IxV curve, so I can adjust its constants. It is given by:
I = I01(exp((V-R*I)/(n1*vth))-1)+I02(exp((V-R*I)/(n2*vth))-1)
vth and R are constants already known, so I only want to achieve I01, I02, n1, n2. The problem is: as you can see, I is dependent on itself. I was trying to use the curve fitting toolbox, but it doesn't seem to work on recursive equations.
Is there a way to make the curve fitting toolbox work on this? And if there isn't, what can I do?
Assuming that I01 and I02 are variables and not functions, then you should set the problem up like this:
a0 = [I01 I02 n1 n2];
MinFun = #(a) abs(a(1)*(exp(V-R*I)/(a(3)*vth))-1) + a(2)*(exp((V-R*I)/a(4)*vth))-1) - I);
aout = fminsearch(a0,MinFun);
By subtracting I and taking the absolute value, the point where both sides are equal will be the point where MinFun is zero (minimized).
No, the CFTB cannot fit such recursively defined functions. And errors in I, since the true value of I is unknown for any point, will create a kind of errors in variables problem. All you have are the "measured" values for I.
The problem of errors in I MAY be serious, since any errors in I, or lack of fit, noise, model problems, etc., will be used in the expression itself. Then you exponentiate these inaccurate values, potentially casing a mess.
You may be able to use an iterative approach. Thus something like
% 0. Initialize I_pred
I_pred = I;
% 1. Estimate the values of your coefficients, for this model:
% (The curve fitting toolbox CAN solve this problem, given I_pred)
I = I01(exp((V-R*I_pred)/(n1*vth))-1)+I02(exp((V-R*I_pred)/(n2*vth))-1)
% 2. Generate new predictions for I_pred
I_pred = I01(exp((V-R*I_pred)/(n1*vth))-1)+I02(exp((V-R*I_pred)/(n2*vth))-1)
% Repeat steps 1 and 2 until the parameters from the CFTB stabilize.
The above pseudo-code will work only if your starting values are good, and there are not large errors/noise in the model/data. Even on a good day, the above approach may not converge well. But I see little hope otherwise.
I did calculation and got the following numbers
0.739128438976901 0.739128438976900
I want MATLAB to consider that they are equal, but MATLAB recognized that the first one was greater than the second. How can I make MATLAB consider them as they are equal ?
Thanks
x = 42
y = 42.00001
if abs(x-y) < tolerance
% do something
end
The setting for tolerance is up to you.
I don't know a whole lot about Matlab (I'm more of a Mathematica guy myself), but it seems there is a roundn(x,n) function which rounds an element x to the nearest multiple of 10^n. Perhaps this could be used here.