convert code from mathematica to matlab

17 views (last 30 days)
david
david on 15 Jun 2023
Edited: Walter Roberson on 21 Jan 2025
hello,
need help to convert the folowing code in wolfarm mathematica to matlab,
i try use ToMatlab function, it's nork for me.
the code is:
V1[Ival_] := (volt = Vmax;
While[ Abs[Id1[volt] - Ival] > .005,
Ivolt = Id1[volt];
slope = (Id1[volt] - Id1[volt - \[Delta]])/\[Delta];
volt = volt - (Ivolt - Ival)/slope;
];
volt
Thx
  2 Comments
chicken vector
chicken vector on 15 Jun 2023
In Matlab you can't have undefined variables like in Wolfram, unless you use symboli toolbox.
If you want to pass your code you have to define them.
What are Id1, Ival and Delta. Are they values, vectors, arrays, functions?
david
david on 16 Jun 2023
Tnx,
delta is value,
Id1, Ival= array

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 16 Jun 2023
Edited: Walter Roberson on 21 Jan 2025
In Mathematica, that code defines an anonmymous function of one parameter that runs a while loop to do a calculation, and then it assigns the anonymous function to the name V1
MATLAB permits defining anonymous functions, but it makes it difficult to define a while loop inside an anonymous function.
In MATLAB, you would be better off defining a true function using function that accepts parameters of Ival, Vmax, Delta, slope . You might later want to use parameterization http://www.mathworks.com/help/matlab/math/parameterizing-functions.html to specialize the function.
Id1, Ival= array
It has been some time since I used Mathematica heavily; did they eventually add the ability to index arrays at non-integer elements? The code is obviously an implementation of Newton's Method using a numeric approximation of derivative to find a root of function Id1 and it obviously expects scalar Ival and function Id1
In MATLAB, unless you had a specific reason otherwise, you would typically use fzero . ("Specific reason" could include that the function generates complex values.)
  1 Comment
david
david on 16 Jun 2023
Edited: david on 16 Jun 2023
Thanks about help.
yes, probably in mathematics this option exists.
i try it's

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!