Estimating a function in matlab using different values of parameters froma matrix/dataset
Afficher commentaires plus anciens
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?
Réponse acceptée
Plus de réponses (1)
madhan ravi
le 5 Jan 2019
0 votes
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 commentaires
Alex Karakostas
le 5 Jan 2019
Modifié(e) : Alex Karakostas
le 5 Jan 2019
madhan ravi
le 5 Jan 2019
Please upload the script your trying also I have no clue why your function requires input when you have defined everything inside the function.
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)))
madhan ravi
le 5 Jan 2019
oh, or lookup vpasolve()
vpasolve(f,a)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!