Show older comments
Hi,
I have a semilog graph which must be fitted (in its linear region) using this equation: y = 6e17*B*log[(x+B)/B]
Can you please tell how can I obtain the value of constant B, using fun(@x) ?
Thank you so much in advance for your help !

Answers (2)
Ameer Hamza
on 26 Jun 2018
Edited: Ameer Hamza
on 26 Jun 2018
If you have a vector of x and y values then you can use several functions to estimate B. The correct method to use depending on your definition of the error function. For example, if you want to estimate B by minimizing the MSE (mean square error) then use lsqcurvefit(). For example,
xdata = ...; % vector of x values
ydata = ...; % vector of y values
y = @(B, x) 6e17*B.*log((x+B)./B);
B_estmated = lsqcurvefit(y, 1, xdata, ydata);
^ initial point for the numerical optimization algorithm.
Similarly, if you have some other error function, then you can use fmincon().
11 Comments
aymos
on 26 Jun 2018
Rik
on 26 Jun 2018
This already is an iterative process. Can you explain what you mean by being prone to error?
Ameer Hamza
on 26 Jun 2018
It seems we again ran into a case of xyproblem. You actually want to solve a different problem but you mentioned something else in your question. Also, you said the plot is between y vs t but you are actually showing the plot between x and y.
Also, it seems that you want to animate the plots for all values of the iterative search process. You can refer to this answer to see how to get output data from numerical optimization toolbox functions. If you still face some problem then please attach a sample dataset with the description of the problem you are actually trying to solve.
Ameer Hamza
on 27 Jun 2018
The following code tries to estimate the value of A, B and C by solving the ode you gave. But the results are far from satisfactory. The optimization algorithm is unable to find a good minimum. It seems that the given data does not match the model you gave.
dataTable = readtable('matlab.txt');
t = dataTable.t;
y = dataTable.y;
y0 = y(1);
errFun = @(A, B, C) sum((odeSolve(A, B, C, t, y0)-y).^2);
estimated_param = fmincon(@(x) errFun(x(1), x(2), x(3)), [10 0 0], [], []);
function dydt = odefun(A, B, C, y)
dydt = A*(B-y)*exp(-C*y)-29*y;
end
function y = odeSolve(A, B, C, t, y0)
[~, y] = ode45(@(t,y) odefun(A, B, C, y), t, y0);
end
Ameer Hamza
on 27 Jun 2018
The algorithm is just returning the initial point. See the second input to the fmincon(). If you also read the text displayed by optimizer it says
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
So the objective function is non-decreasing in the feasible direction. It is not able to decrease the error between estimated model and actual data.
If the model and data are correct then you need to try different starting points instead of [10 0 0]. The function seems to be highly non-convex so a good minima might be difficult to find.
aymos
on 27 Jun 2018
aymos
on 27 Jun 2018
Ameer Hamza
on 27 Jun 2018
I am defining an error metric. For example, if you have a vector of predictedOutput and actualOutput, then you want your predictedOutput to match the actualOutput. Therefore you define an error metric like this
error = predictedOutput - actualOutput;
Sum(error.^2)
the square of error is the most commonly used error metric. Unless there is some additional information given about the error metric, this error metric is used by default. So now if we are able to minimize the value of this error metric, it will mean that difference between predictedOutput and actualOutput is very small.
aymos
on 27 Jun 2018
Ameer Hamza
on 27 Jun 2018
Yes, this equation will give the predicted output y. Also, I realize that using one y together in one statement can be a bit confusing but this syntax is correct. MATLAB does not confuse both y's with each other. You can change either one of the y to another variable name to avoid confusion.
abdul aleem shaik
on 27 Feb 2021
0 votes
I=a1+a2+a3 how to express this in terms of I = fun(ai)
1 Comment
Walter Roberson
on 27 Feb 2021
I = @(ai) sum(ai)
Categories
Find more on Linear Programming and Mixed-Integer Linear Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!