MATLAB equivalent of Mathematica NSolve

In summary, you can use the NSolve function in MATLAB to find the different values of a1, a2, and a3 given your equations and parameters. The syntax is slightly different but the functionality is the same. Additionally, you can use a While loop in Mathematica by using the built-in function "Length" to get the number of iterations.
  • #1
jemma
36
0
Hi all,
In Mathematica I have used the NSolve function to give me all the possible values of a1, a2 and a3, given the parameters I have set for the equations, for example:
Code:
NSolve[{an1 == a1, an2 == a2, an3 == a3, an4 == a4} /. params, {a1, a2, a3}]

Is it possible to do this in MATLAB to find the different values of a1, a2 and a3?

Also, in MATLAB I have a While loop saved as a funtion m-file which is used in another m-file within a For loop that includes a count i=1:1000. I want to do this in Mathematica so that:
Matlab:
For i=1:1000,
(then my while loop function)
If (conditions), count = count+1
End.
How do I do this in Mathematica?
Thanks!
 
Last edited by a moderator:
Physics news on Phys.org
  • #2


Hello,

Yes, it is possible to find the different values of a1, a2, and a3 in MATLAB using the NSolve function. The syntax is slightly different, but the functionality is the same. You can use the following code:

syms a1 a2 a3
eqns = [an1 == a1, an2 == a2, an3 == a3, an4 == a4]; % define your equations
params = [an1, an2, an3, an4]; % define your parameters
sol = solve(eqns, [a1, a2, a3]); % solve for a1, a2, and a3
a1_values = double(sol.a1); % convert symbolic solutions to numerical values
a2_values = double(sol.a2);
a3_values = double(sol.a3);

As for your second question, you can use a While loop in Mathematica as well. The syntax is similar to MATLAB, but instead of using a count variable, you can use the built-in function "Length" to get the number of iterations. Here's an example:

count = 0; % initialize count
While[i < 1000,
(* your while loop code *)
If[conditions, count = count + 1]; % check conditions and increment count if necessary
i++ % increment i
]
count % print the final count

I hope this helps. Let me know if you have any further questions.
 

What is the difference between MATLAB and Mathematica NSolve?

The main difference between MATLAB and Mathematica NSolve is that MATLAB is a programming language and environment primarily used for numerical computing, while Mathematica is a symbolic computation system. This means that MATLAB is better suited for solving numerical problems and performing data analysis, while Mathematica is better for symbolic manipulation and mathematical modeling.

Does MATLAB have an equivalent function to Mathematica NSolve?

Yes, MATLAB has an equivalent function called fsolve which can be used for solving systems of nonlinear equations. It is also possible to use the fzero function for finding roots of a single nonlinear equation.

How does the syntax of MATLAB's fsolve compare to Mathematica NSolve?

The syntax of MATLAB's fsolve is similar to that of Mathematica's NSolve. Both functions require the user to input the system of equations and initial guesses for the variables. However, the syntax for defining the equations and variables may differ slightly.

Can MATLAB's fsolve solve non-polynomial equations like Mathematica NSolve?

Yes, both MATLAB's fsolve and Mathematica's NSolve can solve non-polynomial equations. However, their approaches may differ and it is important to read the documentation for each function to understand the limitations and best practices for solving different types of equations.

Which function is faster, MATLAB's fsolve or Mathematica NSolve?

The speed of these functions depends on the specific system of equations being solved and the hardware on which they are run. In general, both functions are highly optimized and efficient, so the difference in speed may not be significant. It is recommended to test both functions on a given problem to see which one performs better for your specific case.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
875
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
5K
  • Advanced Physics Homework Help
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
11
Views
758
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top