Undefined variable or function 'f'

조회 수: 1 (최근 30일)
Allen Zhong
Allen Zhong 2020년 1월 19일
답변: Kaashyap Pappu 2020년 1월 23일
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