Solve equation without symbolic math toolbox

조회 수: 132 (최근 30일)
Benoit
Benoit 2014년 3월 19일
댓글: Walter Roberson 2025년 11월 6일 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

채택된 답변

Chris C
Chris C 2014년 3월 19일
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
Benoit
Benoit 2014년 3월 20일
이동: John D'Errico 2025년 9월 4일
Ok , i've got it.
Thank you
David Goodmanson
David Goodmanson 2025년 10월 29일 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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Sudesh
Sudesh 2025년 10월 28일 9:24
편집: Walter Roberson 2025년 10월 29일 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 2025년 11월 6일 6:03

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

  댓글 수: 1
Walter Roberson
Walter Roberson 2025년 11월 6일 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 '=='.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by