現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Speed up MatlabFunction or use alternatives
5 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I am currently processing a big equation (in fact a 36 - Vector with ~15.000 characters each) and am trying to get a function using MatlabFunction. The goal is to receive a function which can be evaluated as quickly as possible, as it's part of a system solved by an ODE solver. The equation itself only contains polynomials to the degree of 3 and square-roots (^3/2 e.g.) but no sin, cos, etc. . Calling MatlabFunction took longer than 18 Hours, and now I had to change the equation and I really don't want to wait for the full 18 Hours. Is setting optimize to false advisable, or will this make my evaluation time worse? Are there other options beside MatlabFunction? I am not very familiar with this kind of stuff as I never had to deal with calculations taking so long.
Any help is greatly appreciated!
23 件のコメント
Finn Busch
2020 年 4 月 19 日
It is needed to use symbolic expressions as it is a calculation which is only to be done once. Precalculating it and using MatlabFunction will boost the speed of the actual solving process (it's part of a flexible multibody system simulation). I can attach the script, if that helps. The goal is to get a function that gives me the solution for actual values as fast as possible, and I found that precalculating it symbolically helps a lot..
Finn Busch
2020 年 4 月 19 日
and the calculation contains derivatives, which would not work numerically.
John D'Errico
2020 年 4 月 19 日
Far too often, these huge things with endless equations result in something mathematically and numerically useless - not to be trusted. It looks impressive, but so what? There may be numerical problems just evaluating that mess. Massive internal subtractive cancellation is not uncommon, so that even while a given computation is mathematically correct, it is numerical garbage.
Can you usea GPU or parallel computations to do something? Sadly not. MatlabFunction is a symbolic tool, it does not work well in parallel. GPUs & parallel tools are GREAT when you need to do the same thing over and over again. Not here.
As I said, it looks impressive. It sounds impressive - taking 18 hours just to process. Computers are great, right?
Good luck. Get some coffee while you wait, and read a good, LONG book.
Finn Busch
2020 年 4 月 19 日
編集済み: Finn Busch
2020 年 4 月 19 日
John I think you are certainly misunderstanding something here. I'm not bragging about the process time it takes, in fact I'd love to reduce it. Other than that it's kind of bold of you to just assume that what I am doing is useless or garbage based on the fact that it takes long to solve.
Thanks for pointing out what's possible and what not though, even though I'd highly appreciate if you could keep your opinion based on random guesses by yourself.
darova
2020 年 4 月 19 日
I could'n handle that. I'm sorry
And what is wrong with numerical calculations? Can i help you with them?
Finn Busch
2020 年 4 月 19 日
Nono, I don't ask you to solve that for me, sorry! But maybe you can spot an error or something that slows the process down significantly without changing the outcome?
darova
2020 年 4 月 19 日
I think there are no significant errors or mistakes. I think those calculations should be numerical. It's madness for symbolic evaluations. I can't even open dude variable!
Finn Busch
2020 年 4 月 19 日
編集済み: Finn Busch
2020 年 4 月 19 日
I agree that usually those calculations should be numerical. However these calculations need to be done each time step while solving an ODE. I want to minimize the time it takes to get these calculations done, so I try to precalculate them symbolically, get a MatlabFunction out of it and just call that on each time step (which is quite fast to my experience). Do you think doing these calculations numerically for each time step will be faster after all? Just to be clear, the PRECALCULATION (the script I sent you) only has to be done once.
EDIT: In addition: it's not the calculations that take so long symbolically, it's the MatlabFunction call..
John D'Errico
2020 年 4 月 19 日
Finn - you misunderstand. Nobody ever said you were bragging. My point is it is too easy to get hung up on the idea that a computer produced these equations, therefore they are correct, and anything that comes from them is meaningful. Remember that any model is merely an approximation of some process. A simpler model may be far more useful, because it is possible to make vaild predictions using it. The art then comes down to which terms are safely excluded.
Anyway, when your code eventually runs, it will produce a result, SOME prediction. You need to heavily validate what you get. Its that result meaningful in any way? Is it consistent with reality? All code like this is a model of some process. But did your model predict something that seems reasonable? The first filter is your eye, as you should know what is realistic. That is not a proof of validity, but just an aid to intuition that you may have done something reasonable.
If you just accept the result as "truth" becuase a computer produced it, then you are asking for trouble. Computers are not infallible. They just do what you ask them to do. As far as you can, then make more conclusive tests to verify the result. It is often true that you can find ways to drive even a complex system to produce an expected simple result. Does your model verify that result?
As I said, don't trust the results from such a complex computation, not until you can verify the computations.
darova
2020 年 4 月 19 日
What about subs? How much does matlabFunction take for calculations?
vars1 = [e(:)', [a, b, h], [x, y, z],lam,mu];
tic
A = subs(dude, vars1, 2*rand(size(vars1)));
double(A)
toc
% Elapsed time is 30.214898 seconds.
Finn Busch
2020 年 4 月 19 日
MatlabFunction takes <<1ms for one calculation.
The model I am using is based on continuum mechanics and mostly validated. I cannot change the model itself as it's part of a thesis in which I compare different models. It is expected to be consistent with reality and I have validated it for other models.
回答 (1 件)
Steven Lord
2022 年 11 月 28 日
cd(tempdir)
syms a b c d positive
syms x
s = solve(a*x^3+b*x^2+c*x+d == 0, x, MaxDegree=3);
s is a pretty complicated expression, but the final answer can be simplified through the use of temporary sub-expressions. But that simplification can result in longer files (albeit with simpler expressions and shorter lines) and longer time creating the files.
tic
matlabFunction(s, File='cubicSolver1.m', Optimize=true);
toc
Elapsed time is 0.477392 seconds.
dbtype cubicSolver1.m
1 function s = cubicSolver1(a,b,c,d)
2 %cubicSolver1
3 % S = cubicSolver1(A,B,C,D)
4
5 % This function was generated by the Symbolic Math Toolbox version 9.2.
6 % 28-Nov-2022 14:57:43
7
8 t2 = b.^2;
9 t3 = b.^3;
10 t4 = 1.0./a;
11 t7 = sqrt(3.0);
12 t5 = t4.^2;
13 t6 = t4.^3;
14 t8 = (b.*t4)./3.0;
15 t9 = (c.*t4)./3.0;
16 t10 = (d.*t4)./2.0;
17 t11 = -t8;
18 t12 = -t10;
19 t13 = (b.*c.*t5)./6.0;
20 t15 = (t2.*t5)./9.0;
21 t16 = (t3.*t6)./2.7e+1;
22 t14 = -t13;
23 t17 = -t15;
24 t18 = -t16;
25 t19 = t9+t17;
26 t21 = t10+t14+t16;
27 t20 = t19.^3;
28 t22 = t21.^2;
29 t23 = t20+t22;
30 t24 = sqrt(t23);
31 t25 = t12+t13+t18+t24;
32 t26 = t25.^(1.0./3.0);
33 t27 = 1.0./t26;
34 t28 = t26./2.0;
35 t29 = -t28;
36 t30 = t19.*t27;
37 t31 = t30./2.0;
38 t32 = t26+t30;
39 t33 = t7.*t32.*5.0e-1i;
40 s = [t11+t26-t30;t11+t29+t31-t33;t11+t29+t31+t33];
tic
matlabFunction(s, File='cubicSolver2.m', Optimize=false);
toc
Elapsed time is 0.121004 seconds.
dbtype cubicSolver2.m
1 function s = cubicSolver2(a,b,c,d)
2 %cubicSolver2
3 % S = cubicSolver2(A,B,C,D)
4
5 % This function was generated by the Symbolic Math Toolbox version 9.2.
6 % 28-Nov-2022 14:57:43
7
8 et1 = (c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).*1.0./((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0);
9 et2 = ((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0);
10 et3 = ((c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).*1.0./((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0))./2.0+sqrt(3.0).*(et1+et2).*5.0e-1i-b./(a.*3.0);
11 et4 = ((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0).*(-1.0./2.0);
12 et5 = (c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).*1.0./((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0);
13 et6 = ((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0);
14 et7 = ((c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).*1.0./((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0))./2.0-sqrt(3.0).*(et5+et6).*5.0e-1i-b./(a.*3.0);
15 et8 = ((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0).*(-1.0./2.0);
16 et9 = -(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).*1.0./((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0)-b./(a.*3.0);
17 et10 = ((d.*(-1.0./2.0))./a-(1.0./a.^3.*b.^3)./2.7e+1+sqrt((d./(a.*2.0)+(1.0./a.^3.*b.^3)./2.7e+1-(1.0./a.^2.*b.*c)./6.0).^2+(c./(a.*3.0)-(1.0./a.^2.*b.^2)./9.0).^3)+(1.0./a.^2.*b.*c)./6.0).^(1.0./3.0);
18 s = [et9+et10;et7+et8;et3+et4];
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)