How to find an unknown parameter from a equation fitted to a data-set in MATLAB 2016b ?

I have a data set for x values and y values.Now I want to solve an unknown parameter n from the equation y=2.t(1-cos(x)(n-1)/(n.p-p(1-cos(x))),where t,p are known.How to write the code to get value of n from the fitted plot? Thanks in advance

 Accepted Answer

So, you have this equation:
y = 2*t*(1-cos(x)*(n-1)/(n*p-p*(1-cos(x)))
I've rewritten it above, putting in multiplies where you used . or you used nothing at all. Thus cos(x)(n-1) is not a valid expression. You should get used to writing things so they are readable.
The problem is, this is not valid either, as I wrote it, since the parens are unbalanced. See that there are 6 ( characters in that line, and only 5 ) in there. I never added or deleted any parens. That means your expression makes no mathematical sense.
IF you show me how the parens should be written, then I can show you how to compute n. For example, should it have been this:
y = 2*t*(1-cos(x)*(n-1))/(n*p-p*(1-cos(x)))
or this, where the extra parens was added at the end?
y = 2*t*(1-cos(x)*(n-1)/(n*p-p*(1-cos(x))))
The two expressions are significantly different.
In the first case, solving for n, I would get this relation:
n = (2*t*(1 + cos(x)) + p*y*(1 - cos(x)))/(p*y + 2*t*cos(x))
In the second case (assuming my algebra was correct at this time of day) I might have gotten this:
n = (p*y*(1 - cos(x)) + 2*t*cos(x) - 2*p*t*(1 - cos(x)))/(p*y - 2*p*t + 2*t*cos(x))
But you have multiple values for x and y, thus pairs of points such that these equations should hold true.
So load in your data.
load F:\michel.dat
x=michel(:,1);
y=michel(:,2);
Then compute the mean, but in those expressions, use ./ instead of /, and .* instead of * in the correct expression. The dotted operators are element-wise multiplies and divides.
You don't need .* between two scalar values, but whenever you have vectors in the expression, you need to use the element-wise operators.
So in the first expression, I would get:
n = mean((2*t*(1 + cos(x)) + p*y.*(1 - cos(x)))./(p*y + 2*t*cos(x)));
In the second case, it should be this:
n = mean((p*y.*(1 - cos(x)) + 2*t*cos(x) - 2*p*t*(1 - cos(x)))./(p*y - 2*p*t + 2*t*cos(x)));
The idea is that the simplest estimator of n is simply the arithmetic mean of all of those possible results for n, given all the sets of x and y.
In fact though, if your original equation had noise in it, I pulled a fast one, because the best estimator of n should really be estimated using a nonlinear least squares estimate, based on the original equation. We would get a subtly different result from computing the mean as I did above.
Had you attached your data as a .mat file to a comment, or to your original question, I could now show how to do all of this more properly as a nonlinear least squares estimation. But first, tell me where the extra paren was supposed to be, as I don't want to do it all twice.

4 Comments

Very comprehensively written.Sample mean is the simplest of estimators-agreed. Sanjib uploading the mat file would greatly help moreover he wants to get this from the plot. I dont see the connection. Do you?
If ALL you have is a plot, then just extract the points. I'd just use the xdata and ydata fields, assuming the plot is just a simple plot of the data.
plot(rand(10,1),rand(10,1),'-')
C = get(gca,'children')
C =
Line with properties:
Color: [0 0 1]
LineStyle: '-'
LineWidth: 0.5
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: [0.8909 0.95929 0.54722 0.13862 0.14929 0.25751 0.84072 0.25428 0.81428 0.24352]
YData: [0.92926 0.34998 0.1966 0.25108 0.61604 0.47329 0.35166 0.83083 0.58526 0.54972]
ZData: [1×0 double]
Show all properties
X = C.XData
X =
0.8909 0.95929 0.54722 0.13862 0.14929 0.25751 0.84072 0.25428 0.81428 0.24352
Y = C.YData
Y =
0.92926 0.34998 0.1966 0.25108 0.61604 0.47329 0.35166 0.83083 0.58526 0.54972
Far easier to just use the data before it is plotted. And of course, if there is other stuff in that plot, one needs to be careful to extract only the required data.
Thank you John.I have used the code using mean,but as the data points are getting rounded off in MATLAB,the value is approximate to the actual value.
HUH? What are you talking about?
For example:
X0 = rand(1,5)
X0 =
0.711215780433683 0.22174673401724 0.117417650855806 0.296675873218327 0.318778301925882
Y0 = rand(1,5)
Y0 =
0.424166759713807 0.507858284661118 0.085515797090044 0.262482234698333 0.801014622769739
h = plot(X0,Y0);
h.XData - X0
ans =
0 0 0 0 0
As you can see, when I extracted the data from the plot, I get the EAXCT value. Not a rounded version.
(h.XData - X0) == 0
ans =
1×5 logical array
1 1 1 1 1
I imagine that you are somehow trying to use the values reported to the command window, which will be rounded to 5 significant digits by default. That is simply wrong to do on your part, IF you are doing that. Otherwise, there is NO rounding done, if you CAREFULLY do as I told you to do.

Sign in to comment.

More Answers (1)

I might be wrong here. If you know all of these values and dont know only n cant you just find n through algebra. I mean solving for n from the equation you have given,I get n as n= ((1-cosx)*(p*y-2*t))/((p*y-2*t(1-cos(x))). and now implementing this in Matlab is trivial.

4 Comments

Thanks for answering.I obviously can solve it algebraically but I have to plot graph of y vs x,from there I have to get the value of n.
I dont see the bigger picture. Why do you have to plot it? If you have only the plot but not x and y you can
h = findobj(gca,'Type','line')
x=get(h,'Xdata')
y=get(h,'Ydata')
do this to get the values of x and y from there you can solve this.
load F:\michel.dat
x=michel(:,1);
y=michel(:,2);
index=find(x==200);
y_point=y(index);
solve('n=(2.97-x*0.0000007)*(1-cos(y)))/(2.97*(1-cos(y))-0.0000007*x)',n);
plot(x,y,'b*-');
At first I wrote down this code and showed that 'n' is undefined.Next using your suggestion I wrote down like
load F:\michel.dat
a=michel(:,1);
b=michel(:,2);
plot(a,b,'r*-')
h=findobj(gca,'type','line');
x=get(h,a);
y=get(h,b);
from here I can see the data points which I have given,but not any unknown staff.
Ofcourse.That is expected. As I mentioned in my comment earlier, the code that I gave is for getting data points if you have only the plot and no access to the data points. Since you have the data points this is an parameter estimation problem as pointed out by John

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!