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 Commenti

Rik
Rik il 26 Giu 2018
You should look into the fit function.
aymos
aymos il 26 Giu 2018
Can you please be more elaborate ?

Accedi per commentare.

Risposte (2)

Ameer Hamza
Ameer Hamza il 26 Giu 2018
Modificato: Ameer Hamza il 26 Giu 2018

1 voto

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 Commenti

aymos
aymos il 26 Giu 2018
Hi Ameer... I want to iteratively solve the equation, till I get the value of B.. With the fit, I am always prone to error.. Can you please tell how to do so ?
Rik
Rik il 26 Giu 2018
This already is an iterative process. Can you explain what you mean by being prone to error?
Ameer Hamza
Ameer Hamza il 26 Giu 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
Ameer Hamza il 27 Giu 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
aymos
aymos il 27 Giu 2018
Modificato: aymos il 27 Giu 2018
Hi Ameer ! This code is not clear to me ! The output I am seeing in estimated param are equal to [10 0 0]..
Can you please explain the code ?
errFun = @(A, B, C) sum((odeSolve(A, B, C, t, y0)-y).^2); What is this doing ?
estimated_param = fmincon(@(x) errFun(x(1), x(2), x(3)), [10 0 0], [], []); What is x ?
Can you please tell how can I see the output your code generate ? The data set and the model are correct..
Ameer Hamza
Ameer Hamza il 27 Giu 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
aymos il 27 Giu 2018
So [10 0 0 ] are the initial values of A,B and C ?
aymos
aymos il 27 Giu 2018
Hi Ameer,
Thanks for your inputs, but this not helpful... It is not clear to me what is this doing ? sum((odeSolve(A, B, C, t, y0)-y).^2)
Why is there a power of 2 ?
I guess you are quite an expert, but to me it is not clear what is what in your proposed code, so I am not able to work around it..
Ameer Hamza
Ameer Hamza il 27 Giu 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
aymos il 27 Giu 2018
Thanks Ameer.. and by what variable are you defining the predicted output ? (you are using y for both predicted and actual output?)
[~, y] = ode45(@(t,y) odefun(A, B, C, y), t, y0);
Ameer Hamza
Ameer Hamza il 27 Giu 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.

Accedi per commentare.

abdul aleem shaik
abdul aleem shaik il 27 Feb 2021

0 voti

I=a1+a2+a3 how to express this in terms of I = fun(ai)

Categorie

Tag

Non è stata ancora inserito alcun tag.

Richiesto:

il 26 Giu 2018

Commentato:

il 27 Feb 2021

Community Treasure Hunt

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

Start Hunting!

Translated by