fzero of MATLAB and FindRoot of Mathematica give different results
Dear all, I am writing a small script to solve the temperature dependence of magnetization using a toy model. Mathematically, the problem is to determine the relation between reduced magnetization and reduced temperature . The equation is given by , where is the Brillouin function.
To solve this problem, I transform it into a root-finding problem, specifically . I used MATLAB’s fzero and Mathematica’s FindRoot to solve this equation. However, I noticed that the results from the two methods are not consistent. The result from Mathematica matches the reference, while the MATLAB result also appears reasonable. In both cases, as a function of tends to zero from two different directions as increases.
Is this correct? Could you please comment or share your opinions on this matter?
Here is the MATLAB code:
clearvars; clc; close all; fclose all; format compact; format short;
B = @(J, x) ((2*J+1)/(2*J))*coth(((2*J+1)/(2*J))*x)-(1/(2*J)).*coth((1/(2*J))*x);
J = 1/2;
n = 101;
tau_array = linspace(0, 1.2, n);
m_array = zeros(1, n);
for i = 1:n
tau = tau_array(i);
fun = @(m) B(J, ((3*J)/(J+1))*m*(1/tau)) – m;
x = fzero(fun,[-1, 1]);
m_array(i) = x;
end
fig = figure();
ax = axes(fig);
plot(ax, tau_array, m_array,"LineWidth",2,"Color","red");
xlabel(‘Reudced temperature, tau’);
ylabel(‘Redueced magnetization, m’)
title(‘m as a function of tau’)
ax.TickDir = ‘out’;
Here is the Mathematica code:
Here is the reference result, which is taken from this paper [DOI: 10.1109/ISSE.2008.5276604; https://ieeexplore.ieee.org/document/5276604](See Equation 20 and Figure 3)
Thank you.Dear all, I am writing a small script to solve the temperature dependence of magnetization using a toy model. Mathematically, the problem is to determine the relation between reduced magnetization and reduced temperature . The equation is given by , where is the Brillouin function.
To solve this problem, I transform it into a root-finding problem, specifically . I used MATLAB’s fzero and Mathematica’s FindRoot to solve this equation. However, I noticed that the results from the two methods are not consistent. The result from Mathematica matches the reference, while the MATLAB result also appears reasonable. In both cases, as a function of tends to zero from two different directions as increases.
Is this correct? Could you please comment or share your opinions on this matter?
Here is the MATLAB code:
clearvars; clc; close all; fclose all; format compact; format short;
B = @(J, x) ((2*J+1)/(2*J))*coth(((2*J+1)/(2*J))*x)-(1/(2*J)).*coth((1/(2*J))*x);
J = 1/2;
n = 101;
tau_array = linspace(0, 1.2, n);
m_array = zeros(1, n);
for i = 1:n
tau = tau_array(i);
fun = @(m) B(J, ((3*J)/(J+1))*m*(1/tau)) – m;
x = fzero(fun,[-1, 1]);
m_array(i) = x;
end
fig = figure();
ax = axes(fig);
plot(ax, tau_array, m_array,"LineWidth",2,"Color","red");
xlabel(‘Reudced temperature, tau’);
ylabel(‘Redueced magnetization, m’)
title(‘m as a function of tau’)
ax.TickDir = ‘out’;
Here is the Mathematica code:
Here is the reference result, which is taken from this paper [DOI: 10.1109/ISSE.2008.5276604; https://ieeexplore.ieee.org/document/5276604](See Equation 20 and Figure 3)
Thank you. Dear all, I am writing a small script to solve the temperature dependence of magnetization using a toy model. Mathematically, the problem is to determine the relation between reduced magnetization and reduced temperature . The equation is given by , where is the Brillouin function.
To solve this problem, I transform it into a root-finding problem, specifically . I used MATLAB’s fzero and Mathematica’s FindRoot to solve this equation. However, I noticed that the results from the two methods are not consistent. The result from Mathematica matches the reference, while the MATLAB result also appears reasonable. In both cases, as a function of tends to zero from two different directions as increases.
Is this correct? Could you please comment or share your opinions on this matter?
Here is the MATLAB code:
clearvars; clc; close all; fclose all; format compact; format short;
B = @(J, x) ((2*J+1)/(2*J))*coth(((2*J+1)/(2*J))*x)-(1/(2*J)).*coth((1/(2*J))*x);
J = 1/2;
n = 101;
tau_array = linspace(0, 1.2, n);
m_array = zeros(1, n);
for i = 1:n
tau = tau_array(i);
fun = @(m) B(J, ((3*J)/(J+1))*m*(1/tau)) – m;
x = fzero(fun,[-1, 1]);
m_array(i) = x;
end
fig = figure();
ax = axes(fig);
plot(ax, tau_array, m_array,"LineWidth",2,"Color","red");
xlabel(‘Reudced temperature, tau’);
ylabel(‘Redueced magnetization, m’)
title(‘m as a function of tau’)
ax.TickDir = ‘out’;
Here is the Mathematica code:
Here is the reference result, which is taken from this paper [DOI: 10.1109/ISSE.2008.5276604; https://ieeexplore.ieee.org/document/5276604](See Equation 20 and Figure 3)
Thank you. fzero, findroot MATLAB Answers — New Questions