What does this error mean? Undefined operator '*'

2 vues (au cours des 30 derniers jours)
Raquel Terrer van Gool
Raquel Terrer van Gool le 21 Avr 2020
Hello,
I am trying to multiply a function to later on integrate and I keep getting this error:
Undefined operator '*' for input arguments of type 'function_handle'.
Error in HW11_RaquelTerrerVanGool_1 (line 19)
F=2*pi*r*f
And this is what I have so far:
r=[0 1 2 3 4 5 6 7 8]; % Radius in inches
vf=[3.00 2.92 2.78 2.61 2.36 1.78 1.40 0.67 0.00]; % Velocity in ft/s
v=vf*12; % Velocity converted to in/s
R=8 % Radius of the pipe in inches
f=polyfit(r,v,4);
f=@(r)(f(5)+f(4)*r+f(3)*r.^2+f(2)*r.^3+f(1)*r.^4);
for r=0:length(r)
F=2*pi*r*f
end
Q=int(F,0,R)
Can someone help me please? Thank you very much.
  2 commentaires
John D'Errico
John D'Errico le 21 Avr 2020
By the way, this will get you in deep trouble one day, when you try to debug what is happening.
f=polyfit(r,v,4);
f=@(r)(f(5)+f(4)*r+f(3)*r.^2+f(2)*r.^3+f(1)*r.^4);
So you create the vector f as a vector if coefficients of a function. Then you create a function handle named f, using the vector f itself. It is just pleading for errors to arise. And then, what if you decide to execute that line again, but without recreating f as a vector?
f=@(r)(f(5)+f(4)*r+f(3)*r.^2+f(2)*r.^3+f(1)*r.^4);
oops! f(5) is now a call to that function handle that you just created.
It is code that just begs for a massive headache one day for you.
Likewise, things like
r=[0 1 2 3 4 5 6 7 8];
for r = 0:length(r )
is another case where you will encounter massive problems. you create r as a vector, then use the same variable name r as a loop construct, based on r itself.
In both cases, f and r become completely different things, based on where in your code you happen to be looking. Doing things like this is asking for trouble. Far better is to just use a new variable name, when you have something different. Variable names are completely free. No charge to use a new one.
But why does your code fail? That is the immediate problem.
f as a function handle is not a function itself. Instead of f, we could have called it Fred, or Barney, so just a name. Think of it as a name for the function you just created. So when you try to multiply f times something, MATLAB gets confused. It does not understand how to multiply the name of something times 2*pi.
However, you could do this:
F=2*pi*r*f(r)
Of course, the next step will then also fail, because that loop just keeps on overwriting F. You have created a scalar F each time through the loop. So the secondary problem will be you wither need to create a FUNCTION F, or a vector F.
And THAT depends on whether you really want to use int, or integrate, or trapz.
INT cannot integrate a vector of values. INT is a symbolic tool.
TRAPZ cannot integrate a function. It works on vectors of numbers.
INTEGRATE is a numerical integration tool, that works on a function defined in numerical form.
So whatever you are trying to do, you need to decide which tool you will use.
Raquel Terrer van Gool
Raquel Terrer van Gool le 21 Avr 2020
Noted! I will start doig that, thank you very much for your time!

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 21 Avr 2020
Modifié(e) : Stephen23 le 21 Avr 2020
On this line
f=@(r)...
you defined f to be a function handle of an anonymous function with one input argument. Multiplication is not defined for function handles, only for numeric arrays**. If you want to multiply the output of that function (which will be a numeric array then you will need to call the function so that it returns that array.
Inside the loop you will need to call that function with one input argument, e.g.:
F=2*pi*r*f(r)
% ^^^ call the function with one input argument
Note that your loop simply overwrites F on each iteration, only keeping F from the final iteration. I doubt that that is very useful.
** Well, also for logical and char.

Plus de réponses (0)

Catégories

En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by