Estimating a function in matlab using different values of parameters froma matrix/dataset
3 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
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
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.
More Answers (1)
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.
See Also
Categories
Find more on Assumptions 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!