Find the m of nonlinear equation without initial input m. Please help me, my supervisor ask me to find m without input initial m from right. I know this is nonlinear equation.

4 vues (au cours des 30 derniers jours)
% Given data
sigma = [ 137.478; 162.072; 162.239; 162.818; 164.010; 164.401; 165.872; 167.721; 169.748; 171.390; 177.637; 180.296; 180.572; 181.049; 182.188; 182.862; 187.895; 187.924; 188.066; 188.208; 188.251; 189.464; 190.053; 190.818; 190.987; 191.169; 191.622; 213.997; 218.369; 221.124 ];
% number of specimen
n = numel( sigma )

Réponse acceptée

Shubham
Shubham le 4 Sep 2024
Hi Trong,
To solve the nonlinear equation for "m" without an initial guess, you can use MATLAB's "fsolve" or "fzero" functions.
Here's how you can solve it using "fsolve" function:
  • Convert your equation into a MATLAB function that returns zero when the equation is satisfied.
  • Use "fsolve" to find the root, starting with a default or randomized initial guess to ensure convergence.
Below is the MATLAB script to illustrate these steps:
% Given data
sigma = [137.478; 162.072; 162.239; 162.818; 164.010; 164.401; 165.872; ...
167.721; 169.748; 171.390; 177.637; 180.296; 180.572; 181.049; ...
182.188; 182.862; 187.895; 187.924; 188.066; 188.208; 188.251; ...
189.464; 190.053; 190.818; 190.987; 191.169; 191.622; 213.997; ...
218.369; 221.124];
n = numel(sigma);
% Define the function to solve
fun = @(m) (sum(log(sigma) .* (sigma.^m)) / sum(sigma.^m)) - (1/m) - (sum(log(sigma)) / n);
% Solve using fsolve
options = optimset('Display', 'off'); % Suppress output
m_initial_guess = 1; % You can vary this guess or use different techniques
m_solution = fsolve(fun, m_initial_guess, options);
fprintf('The solution for m is: %.4f\n', m_solution);
The solution for m is: 10.6505
Refer to the following MathWorks documentation link for more information on "fsolve":
Hope this helps.
  3 commentaires
Torsten
Torsten le 4 Sep 2024
The result should be 0, not 1/m. The term 1/m is already subtracted from the right-hand side in the function definition.

Connectez-vous pour commenter.

Plus de réponses (1)

John D'Errico
John D'Errico le 4 Sep 2024
Modifié(e) : John D'Errico le 4 Sep 2024
Not homework, but barely any effort made. Sigh. This is apparently your job. You need to start learning MATLAB.
sigma = [ 137.478; 162.072; 162.239; 162.818; 164.010; 164.401; 165.872; 167.721; 169.748; 171.390; 177.637; 180.296; 180.572; 181.049; 182.188; 182.862; 187.895; 187.924; 188.066; 188.208; 188.251; 189.464; 190.053; 190.818; 190.987; 191.169; 191.622; 213.997; 218.369; 221.124 ];
First, create a function that evaluates this expresssion, as a function of m, and incidentally sigma. I'll precompute the log of the sigma vector, since it will be used often.
Note that the last term is simply the mean of the vector log(sigma). I could probably precompute that too.
logsigma = log(sigma);
fun = @(m) -1/m + sum(logsigma.*sigma.^m)/sum(sigma.^m) - mean(logsigma);
Note that fun is just one equation, as a function of one variable. It returns a scalar value. Before you do ANYTHING, verify that it returns something that makes sense.
fun(1)
ans = -0.9908
fun(3)
ans = -0.3062
Now, PLOT THE FUNCTION! ALWAYS PLOT EVERYTHING. Make sure you understand what is happening.
fplot(fun,[0,5])
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
grid on
xlabel m
Hmm. It looks like it MAY cross zero, for some larger value of m. Best if I redo the plot, changing the axes a bit to target the region I care about.
By the eay, the warning about vectorization is just a warning. fplot wants to be able to pass in multiple values for m all at once, and this objective is not vectorized. In this case, for a simple plot, I don't really care.
fplot(fun,[1,20])
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
grid on
xlabel m
ylim([-1,1])
Do you see what I am doing? Before I throw something nilly willy into a solver, I want to see what is happening, to visualize if a solution even exists.
Finally, what solver do I use? FZERO! fzero is designed to solve nonlinear rootfinding problems with one unknown variable, and one equation. It is designed to be both efficient and robust against difficult problems. It is especially good if you can bound the solution between two limits, known as a bracket, although it would still often work quite well if I gave it only one starting point.
[xsol,fval,exitflag] = fzero(fun,[1,20])
xsol = 10.6508
fval = 0
exitflag = 1
So fzero is happy. exitflag=1 is good. The objective is zero, to within tolerances.

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by