Undefined function 'abs' for input arguments of type 'function_handle'

Hi guys! I am getting this error when I try :
warning ('off','all');
beta1=0.2;
alphabar=-0.1;
theta4range=-1:0.1:4;
h4range=0:0.1:1;
c13range=-1:0.1:1;
% c23=-c13;
rmass=1.0;
rmomi= 2.0;
eps=0.01;
hassolution=0;
for theta4=theta4range
for h4=h4range
for c13=c13range
syms x;
H0=@(x) 1/2*(1-beta1.*x);
H14 =@(x)(-h4-theta4.*(x-1./2)+beta1);
H24 =@(x)(h4 + theta4.*(x-1./2));
u13=@(x)1./H0.* -4.* integral(H14,0,x) +c13;
u23=@(x)1./H0.* -4.* integral(H24,0,x) -c13;
p12 = @(x) -3.* integral(u13,0,x);
p22 = @(x) -3.* integral(u23,0,x);
kutta = @(x) 3.* integral(u23 - u13,0,1)+beta1.*alphabar;
lateral = @(x) integral(p22 - p12,0,1)-12.*rmass.*h4;
angular = @(x) integral((x-1./2).*(p22 - p12),0,1)-12.*rmomi.*theta4;
if abs(kutta)<eps & abs(lateral)<eps & abs(angular)<eps
c13
c23
theta4
h4
hassolution=1;
end
end
end
end
if ~hassolution
disp('No solution');
end

Respuestas (1)

John D'Errico
John D'Errico el 15 de Mzo. de 2015
Editada: John D'Errico el 15 de Mzo. de 2015
It is clearly time for you to learn about functions and how to call them. Start reading the basic tutorials.
Anyway, READ the error message. What is kutta? For that matter, what is lateral? And what is angular?
Answer: They are all function handles. They are NOT numbers. What is the absolute value of a number? That I can do. What is the absolute value of a function handle? Meaningless.
There is a difference between the function handle kutta as you have defined it, and the VALUE of that function on some input like kutta(x), where x is a variable.
By the way, why do you think you need to define the variable x as a symbolic one BEFORE you defined those function handles? There is no need to do so. That line of code was just a waste of CPU cycles.
What I don't know is if you actually want to do symbolic computation on those expressions, or if you want to do numerical computations. If your goal is symbolic computation, then all of those function handles were the wrong thing to do.

5 comentarios

Meva
Meva el 15 de Mzo. de 2015
Editada: Meva el 15 de Mzo. de 2015
well I have a code from my colleaque that is why I have defined x as a symbolic variable and all the other functions. His code is such that :
% script for solving non-linear equations using brute force approach
warning ('off','all');
theta0range=0:0.1:4;
h0range=0:0.1:1;
c1range=0:0.1:4;
eps=0.01;
hassolution=0;
for theta0=theta0range
for h0=h0range
for c1=c1range
firstexp=c1^2-2*c1^2*h0-c1^2*theta0-h0^2-h0*theta0-theta0^2/4+2*c1*h0^2+2*h0*c1*theta0+c1*theta0^2/2;
syms x;
fun1=@(x)(1-c1).^2./(1-h0-(x-1./2).*theta0-0.1.*sin(pi.*x)).^2;
fun2=@(x)c1.^2./(h0+(x-1./2).*theta0-0.1.*sin(pi.*x)).^2;
fun3=@(x)(x-1./2).*((1-c1).^2./(1-h0-(x-1./2).*theta0-0.1.*sin(pi.*x)).^2-c1.^2./(h0+(x-1./2).*theta0-0.1.*sin(pi.*x)).^2);
secondexp=0.5*integral(fun1,0,1)-0.5*integral(fun2,0,1);
thirdexp=0.5*integral(fun3,0,1);
if abs(firstexp)<eps & abs(secondexp)<eps & abs(thirdexp)<eps
theta0
h0
c1
hassolution=1;
end
end
end
end
if ~hassolution
disp('No solution');
end
As far as I know, this code works and I modified this code to my problem. Any suggestion?
My x is varied from 0 to 1 with the increment of 0.01.
Yeah, but your colleague evaluated the functions, then used those results whereas your code uses the function handles as if they were those resulting results. Read the two pieces of code carefully in comparison to each other.
John D'Errico
John D'Errico el 15 de Mzo. de 2015
Editada: John D'Errico el 15 de Mzo. de 2015
And do you understand the difference between his code and yours? (Even if we ignore the fact that he too has no comprehension of the lack of need for that syms line. It is just a waste of cpu cycles in his code too.)
See that the tests in HIS code are applied to variables, to NUMBERS.
What is firstexp, secondexp, thirdexp? They are all NUMBERS. NOT function handles. They are terrible examples of coding too, as naming your variables like that is a great way to write unreadable code.
I should point out that the code you got from your friend is a terribly poor piece of code to "solve" a nonlinear system of equations. I would suggest that your friend needs to learn about tools for solving nonlinear systems of equations. There are MANY of them available. You might start with fsolve.
Warnings are extremely useful when running Matlab. Therefore warning ('off','all') is a very bad idea.
Jan makes a superb point. Warnings are there for a good reason. Disabling them is the equivalent to closing your eyes and your ears, and pretending that nothing is wrong. I see nothing. I hear nothing...

Iniciar sesión para comentar.

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 15 de Mzo. de 2015

Comentada:

el 15 de Mzo. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by