fzero of MATLAB and FindRoot of Mathematica give different results

52 次查看(过去 30 天)
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, m 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.

采纳的回答

Malay Agarwal
Malay Agarwal 2024-6-27,8:36
编辑:Malay Agarwal 2024-6-27,8:39
You're seeing a different graph since you're finding the root around the initial value [-1, 1] in MATLAB while the root is around the initial value 1 in Mathematica. If you change your call to "fzero" from:
x = fzero(fun, [-1, 1]);
To:
x = fzero(fun, 1);
The graph is as expected. Here's the complete code for your reference:
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); % Note the change from [-1, 1] to 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';
Please note that only the y-axis limits are different from the expected graph. You can change it easily using the "ylim" function.
Please refer to the following links for more information:
Hope this helps!

更多回答(0 个)

类别

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

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by