convert code from mathematica to matlab

16 visualizzazioni (ultimi 30 giorni)
david
david il 15 Giu 2023
Modificato: Walter Roberson il 21 Gen 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 Commenti
chicken vector
chicken vector il 15 Giu 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 il 16 Giu 2023
Tnx,
delta is value,
Id1, Ival= array

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 16 Giu 2023
Modificato: Walter Roberson il 21 Gen 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 Commento
david
david il 16 Giu 2023
Modificato: david il 16 Giu 2023
Thanks about help.
yes, probably in mathematics this option exists.
i try it's

Accedi per commentare.

Categorie

Scopri di più su MATLAB 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