Need help with converting Mathematica code to MATLAB code

조회 수: 2 (최근 30일)
Deepa Maheshvare
Deepa Maheshvare 2022년 7월 4일
답변: Aditya 2023년 10월 9일
I have a dataset with values of multiple curves. An example plot is shown below. I want to scale the curves (up/down) so that all curves overlap or get closer to the target curve. The following is a sample dataset and
And the curves look like the below after minimizing the different between a curve and the target curve.
The following is the Mathematica code with generates the output in the second figure. (code and mathematical functions posted here https://scicomp.stackexchange.com/questions/41552/minimize-distance-between-curves)
data = {{{157, 60}, {140, 57}, {121, 52}, {103, 47}, {86, 39}, {67, 28}, {50,21}, {32, 15}}, {{159, 70}, {140, 67}, {122, 63}, {105, 58}, {85, 45}, {68, 42}, {50, 34}, {33, 29}}, {{157, 74}, {140, 71}, {121, 70}, {105, 65}, {86, 55}, {69, 52}, {52, 43}, {32, 37}}, {{158, 100}, {141, 100}, {123, 95}, {105, 90}, {88, 78}, {69, 72}, {51, 57}, {33, 46}}, {{160, 143}, {140, 146}, {123, 146}, {105, 145}, {86, 136}, {70, 121}, {51, 97}, {34, 60}}}
gr1 = ListPlot[data, AspectRatio -> 1.3];
gr2 = ListLinePlot[data, AspectRatio -> 1.3];
Show[gr1, gr2]
min = 1000000;
For[r = 1, r <= 5, r++,
obj = Total[Table[If[k != r, Sum[(data[[k]][[j, 2]] a[k] + b[k] - data[[r]][[j, 2]])^2, {j, 1, 8}], 0], {k, 1, 5}]];
vars = Flatten[Table[If[k != r, {a[k], b[k]}], {k, 1, 5}]];
sol = Minimize[obj, vars];
If[sol[[1]] < min, rmin = r; min = sol[[1]]; coefs = sol[[2]]]
]
sdata = Table[Table[{data[[k]][[j, 1]], data[[k]][[j, 2]] a[k] + b[k]} /. coefs, {j, 1, 8}], {k, 1, 5}] /. {a[rmin] -> 1, b[rmin] -> 0}
gr1b = ListPlot[sdata, AspectRatio -> 1.3];
gr2b = ListLinePlot[sdata, AspectRatio -> 1.3];
Show[gr1b, gr2b]
I would like to ask for help in converting this to MATLAB code.
  댓글 수: 2
Sam Chak
Sam Chak 2022년 7월 4일
Just wondering. Why did you decide to switch from Mathematica to MATLAB?
Deepa Maheshvare
Deepa Maheshvare 2022년 7월 4일
I want to integrate with othe functions that are available in MATLAB

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

답변 (1개)

Aditya
Aditya 2023년 10월 9일
Hi Deepa,
From your query, I understand that you would like to convert a Mathematica program to MATLAB script. As of now MATLAB does not have any direct interface with Mathematica. However, there is a user-contributed file available on MATLAB Central at the following URL:
This file is not owned by MathWorks. If you have any questions or encounter any issues, kindly reach out to the author for assistance.
You can also use ToMatlab package built for Mathematica. Please refer to the following link for more information:
Here is an example for the same:
Place the file in applications folder of your base directory.
<< ToMatlab
some_expression ToMatlab
Hope this helps!

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by