Undefined variable or function 'f'

1 次查看(过去 30 天)
Hi, I'm trying to resolve an error in my matlab code, which is trying to find the inductance and resistance through voltage and current. Here are my codes:
function [b,m,db,dm] = wRegression(x, y, dy)
% WREGRESSION [b,m,db,dm] = wRegression(x, y, dy)
% Written by: Allen Zhong
% takes x and y values, and uncertainies on y (dy), and outputs the slope
% (m), intercept (b), slope error (dm), and intercept error (db) for the
% linear regression (y = mx + b)
%define the variable w to simplify expressions
w = 1./(dy.^2);
D = addUp(w).*addUp(w.*x.^2)-(addUp(w.*x)).^2;
b = (addUp(w.*x.^2).*addUp(w.*y)-addUp(w.*x).*addUp(w.*x.*y))/D; %Computes y intercept
m = (addUp(w).*addUp(w.*x.*y)-addUp(w.*x).*addUp(w.*y))/D; %Computes slope
db = sqrt(addUp(w.*x.^2)/D); %Computes uncertainties in intercept
dm = sqrt(addUp(w)/D); %Computes uncertainties in slope
end
function [L, R, dL, dR] = findInductanceAndResistance(f, V, I, dV, dI)
wsq = (2.*pi.*f).^2;
Z2 = (V./I).^2;
dzSqrd = ((Z2(V+dV,I)-Z2(V,I)).^2+(Z2(V,I+dI)-Z2(V,I)).^2).^0.5;
[Rsq,Lsq,dLsq,dRsq] = wRegression(wsq,Z2,dzSqrd);
L= (Lsq).^0.5;
R = (Rsq).^0.5;
dR = (Rsq+dRsq).^0.5-(Rsq).^0.5;
dL = (Lsq+dLsq).^0.5-(Lsq).^0.5;
end
For 'findInductanceAndResistance', I can't seem to get it working as there is always an unresolved 'undefined function 'f''. Any help and assistance would be appreciated.
  2 个评论
Star Strider
Star Strider 2020-1-19
Siunce ‘f’ is an agrument (that appears to be a frequency vector), it must specifically be passed to the function. Functions written using the function declaration (as opposed to anonymous functions) do not inherit variables from the workspace.
Stephan
Stephan 2020-1-19
How do you call this function?

请先登录,再进行评论。

回答(1 个)

Kaashyap Pappu
Kaashyap Pappu 2020-1-23
The variable ‘f’ needs to be passed as an input argument in the function call. As has been mentioned, each function has its own workspace and therefore will not access variables from the base workspace. More information can be found here.
If ‘f’ is a custom function, you may have to make sure the function is saved in the same directory while calling ‘findInductanceAndResistance’.
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by