How to run function within for loop
조회 수: 1 (최근 30일)
이전 댓글 표시
The code given below solves the delayed type differential equation. I want to run this code for >10,000 different combinations of
and
. That is,
and
;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/209715/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/209716/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/209717/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/209718/image.png)
function [sol]=system_simulation
po=1.4; do=8; tau=0.5; xth=0.2; vth=0.2;ksi=0.25;
option=odeset('RelTol', 1e-4, 'AbsTol', 1e-4);
sol = dde23(@syst,[tau],@hystory,[0 100],option);
figure(1), plot(sol.x,sol.y(1,:));
% --------------------------------------------------------------------------
function s = hystory(t)
% Constant history function for inverted pendulum.
s = [0.4;0.1];
end
% --------------------------------------------------------------------------
function xprime = syst(t,x,Z)
xlag=Z(:,1);
xprime=zeros(2,1);
xprime(1)=x(2);
xprime(2)=x(1)-p*xlag(1)-d*xlag(2);
end
end
댓글 수: 2
Geoff Hayes
2019년 3월 21일
Mirlan - you want to iterate over po and do but nowhere do you reference them. How should they be used? (It is also doubtful you want to plot the results for all 10000 iterations so please discuss what you want to save/store from each iteration.
채택된 답변
Jan
2019년 3월 22일
편집: Jan
2019년 3월 22일
I prefer providing parameters by anonymous functions instead of nested function. See Answers: Anonymous function for params . Then:
pv = 0.5:0.1:14;
dv = 0.5:0.1:14;
Result = cell(numel(pv), numel(dv));
ip = 0;
for p = pv
ip = ip + 1;
id = 0;
for d = dv
sol = dde23(@(t,x,Z) syst(t,x,Z, p, d), tau, ...
@hystory, [0, 100], option);
id = id + 1;
Result{ip, id} = sol.y(1,:);
end
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Delay Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!