Solve equation without symbolic math toolbox

111 visualizzazioni (ultimi 30 giorni)
Benoit
Benoit il 19 Mar 2014
Commentato: Walter Roberson il 6 Nov 2025 alle 18:22
Hello ,
I'm trying to solve this equation :
0 = (U/r.^3)* sqrt(-2+((3*r.^3)*(Bx/U)))-B
U is a constant
Bx and B are column matrix
I would have as a result a value of r for each value of Bx and B
is it possible to do this without the Symbolic math toolbox ?
Thank you

Risposta accettata

Chris C
Chris C il 19 Mar 2014
You can run this with two functions. The first function I have designated as myMain.m and it is written as..
r0 = rand(3,3);
fsolve(@myfun,r0)
The second function myfun.m is called by the first function by passing initial guesses of r as r0. I altered the way your equation above was written and wrote the function as...
function F = myfun(r)
Bx = rand(3,1);
B = rand(3,1);
U = rand(1);
F = (U)*sqrt(-2+((3*r.^3)*(Bx/U)))-r.^3*B;
end
Change the matrices to whatever numbers you need and it should work.
Good luck.
  3 Commenti
Benoit
Benoit il 20 Mar 2014
Spostato: John D'Errico il 4 Set 2025
Ok , i've got it.
Thank you
David Goodmanson
David Goodmanson il 29 Ott 2025 alle 6:13
Am I missing something here? It looks like the OP from awhile ago now was looking for the solution of
(U/r^3)*sqrt(-2 + 3*r^3*Bx/U) - B = 0
for pairs (Bx, B). Since r only comes in as r^3, set r^3 = a. Square both sides and rearrange to obtain
-2 + 3*a*Bx/U - (B/U)^2*a^2 = 0
Solve this with
a = roots([-(B/U)^2 3*(Bx/U) - 2])
then
r = a^(1/3).
Since the equation was squared, you have to go back and determine which of the two roots satisfy the original equation and discard the other one. In exchange for that step there is no guessing around with a solver for what size the numerical solution might be.
Presumably if a is real then the real solution for r is the one that is wanted. If a is complex, then that is the way it is.

Accedi per commentare.

Più risposte (2)

Sudesh
Sudesh il 28 Ott 2025 alle 9:24
Modificato: Walter Roberson il 29 Ott 2025 alle 6:16
can solve this numerically without the Symbolic Math Toolbox. Since you want a value of rrr for each pair of BxBxBx and BBB, you can use numerical root-finding methods like fzero in MATLAB. Here’s a step-by-step guide:
Your equation is:
You want to solve for rrr, given UUU, BxBxBx (a column vector), and BBB (a column vector of the same size).
MATLAB Approach:
U = 1; % Example value
Bx = [0.5; 1.2; 0.8]; % Example column vector
B = [0.1; 0.2; 0.15]; % Example column vector
r_values = zeros(size(Bx)); % Preallocate
for k = 1:length(Bx)
func = @(r) (U./r.^3).*sqrt(-2 + (3*r.^3)*(Bx(k)/U)) - B(k);
% Provide an initial guess for r, say 0.5
r_guess = 0.5;
% Solve numerically
r_values(k) = fzero(func, r_guess);
end
disp(r_values)
Notes:
  1. Initial guess: fzero requires a starting point. You might need to adjust it depending on the expected range of r.
  2. Vectorization: Each r depends on a pair (Bx(k), B(k)), so a for loop is appropriate.

Preetham
Preetham il 6 Nov 2025 alle 6:03

1ans: 0 = (U/r.^3)* sqrt(-2+((3*r.^3)*(Bx/U)))-B

  1 Commento
Walter Roberson
Walter Roberson il 6 Nov 2025 alle 18:22
I do not understand how this solves the question that was asked? It is not even valid MATLAB syntax.
syms U r Bx B
0 = (U/r.^3)* sqrt(-2+((3*r.^3)*(Bx/U)))-B
Incorrect use of '=' operator. Assign a value to a variable using '=' and compare values for equality using '=='.

Accedi per commentare.

Categorie

Scopri di più su Symbolic Math Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by