Undefined variable in a self-written function

Hello everyone,
I am facing a probelm, below is the codes of the function.
function para = MH(para, x)
A = para(1);
mu = para(2);
Ms = para(3);
sigma = para(4);
C = para(5);
kb=1.38065e-23;
T=295;
distribution = @(d) exp(-(log(d/mu).^2/(2*sigma^2)))./(d*sigma*sqrt((2*pi)));
langevin = @(d) coth((d.^3*pi./6*Ms.*x)/(kb*T))-(kb*T)./(pi./6*Ms.*x.*d.^3);
integrand = langevin(d).*distribution(d).*(pi/6)*d.^3;
para = A*integral(@(d) integrand,0,200e-9)+C.*x;
end
When the whole program ran, the erro code says the 'd' is undefined. d is the variable of integration whose limit of integration is (0,200e-9), how should I define 'd' in this situation? This is my first time using 'integral' function.
When it's prossible, may I ask another question? I am trying to fit a M(H) curve, the formulation is:
where
,
the unknown parameters are A, Ms, Sigma, Mu and C.
The dimension of variable x is (61×1).
The problem is, I once successed running the program (by giving d a arbitrary value in this function), but the part 'intergrand' doesn't be integrated at all, the returned parameter Ms, Sigma and Mu doesn't change, only A and C changed, may I ask what's the reason for that? Thanks for any help in advance!

Answers (1)

integrand = @(d) langevin(d).*distribution(d).*(pi/6)*d.^3;
para = A*integral(integrand,0,200e-9)+C.*x;

2 Comments

Hello,
thank you so much for your advice! Ya, it runs without the former erro codes now, the current problem is that the term integrand doesn't be integrated, the operand is only C*x. The result is below:
Do you have any idea about this problem? Thank you so much for your help!
You are integrating over a fairly small area, 0 to 200e-9 . My guess is that the integral is coming out as nearly 0 compared to C.*x

Sign in to comment.

Asked:

on 17 Jun 2021

Commented:

on 19 Jun 2021

Community Treasure Hunt

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

Start Hunting!