function handle is not converting

I'm getting these error:
Conversion to function_handle from double is not possible.
Already searched about it and tried to change my code but without sucess. Could u help? Thanks
A=[99.23;100.05;91;107.71;104.1];
B=[3441 3441 301720.5;68750 1068750 0;170040 13085020 0;229350 229350 5729350;34194000 0 0];
N=[300000;1000000;13000000;5500000;32800000];
E=[-0.00302;-0.00261;-0.00208];
[c3,r3]=size(A);
[c4,r4]=size(B);
x=sym ('x',[1 c3]);
x=transpose(x);
for i=1:c3
Valor(i,1)=0;
for j=1:r4
Valor(i,1)=@(x)((Valor(i,1)/((1+E(j,1)+x(j,1))^j))+(B(i,j)/((1+E(j,1)+x(j,1))^j)));
end
end
What I want is to find the vector x given that I already have a vector Valorantigo that I will use to apply solve.
Valorantigo(1:c3,1)=A(1:c3,1).* N(1:c3,1) ./100;
eqn=Valor(1:c3,1)==Valorantigo(1:c3,1);
[solx, param, cond] = solve(eqn, x, 'ReturnConditions', true);
Basically x would be the solution of
  • Valorantigo(1,1)=3441/(1-0.00302+x1) + 3441/(1-0.00261+x1)^2 + 301720.5/(1-0.00208+x1)^3
  • Valorantigo(2,1)=68750/(1-0.00302+x2) + 1068750/(1-0.00261+x2)^2 + 0/(1-0.00208+x2)^3
  • Valorantigo(3,1)=170040/(1-0.00302+x1) + 13085020/(1-0.00261+x1)^2 + 0/(1-0.00208+x1)^3
  • the same fot the other lines...

2 Comments

What are you trying to do? At the moment you're trying to put a function into a matrix, which is not allowed.
Furthermore even if it was possible, you're creating a different function for each j yet try to put them all in the same matrix element, overwriting the previous function. I assume it's not your intent.
What I'm trying to do is to create a vector Valor , then I have an already build vector Valorantigo that I will use to equal Valor to find the x vector.
Valorantigo{1:c3,1}=A(1:c3,1).* N(1:c3,1) ./100;
eqn={Valor{1:c3,1}}=={Valorantigo{1:c3,1}};
[solx, param, cond] = solve(eqn, x, 'ReturnConditions', true);
Can you help please? I'm killing my brain with this without results :(

Sign in to comment.

Answers (1)

Valor(i,1) is a double, while
@(x){(Valor(i,1)/((1+E(j,1)+x(j,1))^j))+(B(i,j)/((1+E(j,1)+x(j,1))^j))}
is a function handle. (By the way: are you sure you want curly braces here?) You cannot assign a function handle to an element of a double vector.
I'm not sure if it is intented to define "x" as symbolic variable at first, but then to redefine it as input of the anonymous function. It is also very strange, that "Valor(i,1)" appears on the left and right hand side of the assignment.
Because you did not explain, what you want to achieve (comments are useful!), I cannot guess, how the code should be changed.

5 Comments

First of all thanks. What I want is to find the vector x given that I already have a vector Valorantigo that I will use to apply solve.
Valorantigo(1:c3,1)=A(1:c3,1).* N(1:c3,1) ./100;
eqn=Valor(1:c3,1)==Valorantigo(1:c3,1);
[solx, param, cond] = solve(eqn, x, 'ReturnConditions', true);
Basically x would be the solution of
  • Valorantigo(1,1)=3441/(1-0.00302+x1) + 3441/(1-0.00261+x1)^2 + 301720.5/(1-0.00208+x1)^3
  • Valorantigo(2,1)=68750/(1-0.00302+x2) + 1068750/(1-0.00261+x2)^2 + 0/(1-0.00208+x2)^3
  • Valorantigo(3,1)=170040/(1-0.00302+x3) + 13085020/(1-0.00261+x3)^2 + 0/(1-0.00208+x3)^3
  • the same fot the other lines...
Jan
Jan on 17 Jul 2017
Edited: Jan on 17 Jul 2017
Do you need this to be symbolically? Storing the coefficients of the polynomials numerically might be easier.
Do you see the problem, that you cannot assign the symbolic expression to the scalar double element "Valorantigo(1,1)"?
I just used symbollic because I though it was the right way to do it. Because I want to find the x but I can't even make the loop work. Could u make a suggest on how to do it?
I do not have the symbolic toolbox and unfortunately I still do not understand, what you want to achieve. I do not see the meaning of:
create a vector Valor , then I have an already build vector
Valorantigo that I will use to equal Valor to find the x vector
I already have the code for what I want. Now I have another problem:
A=[99.23;100.05;91;107.71;104.1];
B=[3441 3441 301720.5;68750 1068750 0;170040 13085020 0;229350 229350 5729350;34194000 0 0];
N=[300000;1000000;13000000;5500000;32800000];
E=[-0.00302;-0.00261;-0.00208];
[c3,r3]=size(A);
[c4,r4]=size(B);
Valorantigo(1:c3,1)=A(1:c3,1).* N(1:c3,1) ./100;
x=sym ('x',[1 c3]);
x=transpose(x);
for i=1:c3
Valor(i,1)=symfun(0,x);
for j=1:r4
Valor(i,1)=symfun((Valor(i,1)/(1+E(j,1)+x(i,1))^j)+((B(i,j)/((1+E(j,1)+x(i,1))^j))),x);
end
end
eqn=Valor(1:c3,1)==Valorantigo(1:c3,1);
[x1,x2,x3,x4, x5, param, cond] = solve(eqn, x, 'ReturnConditions', true);
But I'm not getting any solutions. Can you understand why?

Sign in to comment.

Asked:

on 17 Jul 2017

Commented:

on 17 Jul 2017

Community Treasure Hunt

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

Start Hunting!