- "fzero" documentation - https://www.mathworks.com/help/releases/R2023a/matlab/ref/fzero.html.
- "ylim" documentation - https://www.mathworks.com/help/releases/R2023a/matlab/ref/ylim.html.
fzero of MATLAB and FindRoot of Mathematica give different results
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
qilin guo
le 27 Juin 2024
Commenté : qilin guo
le 27 Juin 2024
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.
0 commentaires
Réponse acceptée
Malay Agarwal
le 27 Juin 2024
Modifié(e) : Malay Agarwal
le 27 Juin 2024
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!
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Specifying Target for Graphics Output dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!