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

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?

 採用された回答

Stephan
Stephan 2019 年 1 月 5 日
編集済み: Stephan 2019 年 1 月 5 日
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 件のコメント

Alex Karakostas
Alex Karakostas 2019 年 1 月 5 日
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 2019 年 1 月 5 日
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.

サインインしてコメントする。

その他の回答 (1 件)

madhan ravi
madhan ravi 2019 年 1 月 5 日

0 投票

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 件のコメント

Alex Karakostas
Alex Karakostas 2019 年 1 月 5 日
編集済み: Alex Karakostas 2019 年 1 月 5 日
Hi Madhan,
Following your point and Stephan answer I did the following:
Changed the function to:
function [x] = Model_b(x,r,a,j)
syms r x a
f=(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
a = solve(f,a)
end
and then run my loop from before. I now get a calculation.
f =
(2*r - 2)/(90 - x)^r - (4*r - 4)/(2*x + 90)^r + (a*(r - 1))/(2*(90 - x)^r) - (a*(r - 1))/(2*x + 90)^r
a =
-4
The following error occurred converting from sym to double:
Unable to convert expression into double array.
However, there is still something wrong as it does not pass this information in to the a0 matrix.
madhan ravi
madhan ravi 2019 年 1 月 5 日
Please upload the script your trying also I have no clue why your function requires input when you have defined everything inside the function.
Stephan
Stephan 2019 年 1 月 5 日
編集済み: Stephan 2019 年 1 月 5 日
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)))
oh, or lookup vpasolve()
vpasolve(f,a)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2019 年 1 月 5 日

編集済み:

2019 年 1 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by