# fzero of MATLAB and FindRoot of Mathematica give different results

13 Ansichten (letzte 30 Tage)
qilin guo am 27 Jun. 2024
Kommentiert: qilin guo am 27 Jun. 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 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Malay Agarwal am 27 Jun. 2024
Bearbeitet: Malay Agarwal am 27 Jun. 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.
Hope this helps!
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
qilin guo am 27 Jun. 2024
Dear Malay Agarwal, it helps me a lot. Thank you!

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Specifying Target for Graphics Output finden Sie in Help Center und File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!

Translated by