What does this error mean? Undefined operator '*'
古いコメントを表示
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 件のコメント
John D'Errico
2020 年 4 月 21 日
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
2020 年 4 月 21 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Numerical Integration and Differentiation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!