Estimating a function in matlab using different values of parameters froma matrix/dataset

3 views (last 30 days)
THE SITUATION
I have the following function:
(2*(r - 1))/(90 - x)^r - (4*(r - 1))/(2*x + 90)^r + (a*(r - 1))/(2*(90 - x)^r) - (a*(r - 1))/(2*x + 90)^r =0
I have a dataset/matrix with different values of r and x and I wish to estimate "a"
I tried the following:
a) generate a function:
function [x] =Model_b(x,r,a,j)
f=@(x) ((2*(r - 1))/(90 - x)^r - (4*(r - 1))/(2*x + 90)^r...
+ (a*(r - 1))/(2*(90 - x)^r) - (a*(r - 1))/(2*x + 90)^r ==0);
a = solve(f, a )
end
b) try to run a loop:
for i=1:N
r=dataset2(i,3);
x=dataset2(i,2);
a(i)=Model_b(r,x,a);
end
RESULT
1) If I have the "==0"
I get the following error
f =
function_handle with value:
@(x)(2*(r-1))/(90-x)^r-(4*(r-1))/(2*x+90)^r+(a*(r-1))/(2*(90-x)^r)-(a*(r-1))/(2*x+90)^r==0
Error using solve (line 266)
Specify the variable to solve for.
Error in FSdydx_b (line 8)
a = solve(f,a)
2) If I remove "==0" I get the following sequence of errors:
Undefined operator '*' for input arguments of type 'struct'.
Error in Model_b>@(x)((2*(r-1))/(90-x)^r-(4*(r-1))/(2*x+90)^r+(a*(r-1))/(2*(90-x)^r)-(a*(r-1))/(2*x+90)^r) (line 7)
(2*(r - 1))/(90 - x)^r - (4*(r - 1))/(2*x + 90)^r + (a*(r - 1))/(2*(90 - x)^r) - (a*(r - 1))/(2*x + 90)^r...
Error in sym>funchandle2ref (line 1291)
S = x(S{:});
Error in sym>tomupad (line 1204)
x = funchandle2ref(x);
Error in sym (line 211)
S.s = tomupad(x);
Error in solve>getEqns (line 402)
a = sym(a);
Error in solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
Error in model_b (line 10)
a = solve(f, a )
ADDITIONAL COMMENTS
Note that if my function was of the simpler form:
function [x] = Model_y_(x,r,b,j)
b = -(4*x - (90 - x)^r + (2*x + 90)^r - 90)/(90 - x)^r
end
I can do the estimation without a problem. My problem stems from the fact that I cannot initially create such a format so I need to say to Matlab to input the values of r and x and solve for b. Any idea what I am doing wrong?

Accepted Answer

Stephan
Stephan on 5 Jan 2019
Edited: Stephan on 5 Jan 2019
Hi,
no datasets needed i think, a=-4.
syms r x a
fun = (2*(r - 1))/(90 - x)^r - (4*(r - 1))/(2*x + 90)^r + (a*(r - 1))/(2*(90 - x)^r) - (a*(r - 1))/(2*x + 90)^r ==0;
result = isolate(fun,a)
Best regards
Stephan
  2 Comments
Alex Karakostas
Alex Karakostas on 5 Jan 2019
HI Stephan!
Thanks for the quick reply! You are right. It seems that -4 is the solution. Let me generalize the question, as I anticipate I will run to this again.
How do I write a function which says, for function x solve(x,a). I think that the error comes from the way I define the function, as otherwise I should receive the answer -4 in my dataset for all observations.
Best regards,
Alex
PS: Happy New Year!
Stephan
Stephan on 5 Jan 2019
A very good answer to this consecutive question is given by Madhan below. But sometimes Symbolic Toolbox fails if things are getting to complicated, then you should try to solve numeric - for example by using fzero.

Sign in to comment.

More Answers (1)

madhan ravi
madhan ravi on 5 Jan 2019
solve is a part of symbolic math toolbox so the variables have to be defined as of symbolic class so remove @(x) and define it as syms at the beginning as Stephen showed in his answer.
  4 Comments
Stephan
Stephan on 5 Jan 2019
Edited: Stephan on 5 Jan 2019
dont use a in double sense as a numric variable to save result in and as a symbolic variable at the same time - try:
res_a = double(solve(f,a))
or when using isolate:
res_a = double(rhs(isolate(f,a)))

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!