- "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 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
qilin guo
el 27 de Jun. de 2024
Comentada: qilin guo
el 27 de Jun. de 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1723481/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1723486/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1724151/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1723551/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1724156/image.png)
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';
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1723526/image.jpeg)
Here is the Mathematica code:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1723531/image.jpeg)
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1723536/image.gif)
Thank you.
0 comentarios
Respuesta aceptada
Malay Agarwal
el 27 de Jun. de 2024
Editada: Malay Agarwal
el 27 de Jun. de 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!
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!