convert code from mathematica to matlab
17 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
chicken vector
2023 年 6 月 15 日
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?
回答 (1 件)
Walter Roberson
2023 年 6 月 16 日
編集済み: Walter Roberson
2025 年 1 月 21 日
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.)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!