How to speed up the while loop?
4 次查看(过去 30 天)
显示 更早的评论
I have this while loop in the matlab when I try to run the code it is taking forver to run it.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/736359/image.jpeg)
6 个评论
per isakson
2021-9-12
编辑:per isakson
2021-9-12
You haven't provided information enough for us help. Proposal
- See Profile Your Code to Improve Performance
- Upload a code that shows the problem and that we can run. (Formatted text, not an image)
- "stop the run" and run profile viewer
采纳的回答
per isakson
2021-9-12
You didn't test the code before uploading!
I modified the input lines
z = 1; % input('Enter zeta, z = ');
w = 1; % input('Enter omega, w = ');
dt = 0.1; % input('Enter delta_t,dt = ');
Tmax = 10; % input('Enter T_max, Tmax = ');
Added
t_i = t;
before the while-loop. And commented out
% Excit;
since it only contained comments. And modified the last statement in the while-loop
t_i = t_i+dt;
Now it runs in no time
>> tic, main, toc
Elapsed time is 0.005256 seconds.
2 个评论
per isakson
2021-9-12
"w,z, and dt in the command line" I guess that means you should convert the script to a function, see Scripts vs. Functions.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!