fun(@x)
이전 댓글 표시
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 !

답변 (2개)
Ameer Hamza
2018년 6월 26일
편집: Ameer Hamza
2018년 6월 26일
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
aymos
2018년 6월 26일
Rik
2018년 6월 26일
This already is an iterative process. Can you explain what you mean by being prone to error?
Ameer Hamza
2018년 6월 26일
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
2018년 6월 27일
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
2018년 6월 27일
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
2018년 6월 27일
aymos
2018년 6월 27일
Ameer Hamza
2018년 6월 27일
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
2018년 6월 27일
Ameer Hamza
2018년 6월 27일
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
2021년 2월 27일
0 개 추천
I=a1+a2+a3 how to express this in terms of I = fun(ai)
댓글 수: 1
Walter Roberson
2021년 2월 27일
I = @(ai) sum(ai)
카테고리
도움말 센터 및 File Exchange에서 Linear Programming and Mixed-Integer Linear Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!