Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
functions with for loop
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi all, I have a function for some heat transfer model and it works just fine. The out put of the function is an nxm matrix. The next step for me is to run that function in for loop and get multiple nxm matrices. The code is long but I'll put a short example here for illustration.
function [T] = heat(x,y) % xa and y are nxm matrices
T = X+T0;
end
now what if i want to run this code 10 times by changing the value of T0 to the previous value of T as follows
function [T] = heat(x,y)
for i=1:10
T = X+T0;
T0=T;
end
end
how can get the 10 matrices of T as an output? because running this code doesn't work
댓글 수: 0
답변 (1개)
dpb
2017년 6월 19일
Your heat transfer model has to be dependent upon something else to cause it to return different values; you show arguments (x,y) which one presumes are the geometry. What is the variable you want to change in these three runs; it can't be the output temperature itself, it has to be something in the model.
You need to move whatever is/are that/those parameter/s into the argument list so you can set its/their values when you call it in the loop. Then the loop could look something like
VarToChange=V0; dV=dV0; % initial values of the variable(s) and the change/increment
N=10; % the number of loops
T=zeros(n,m,N); % preallocate the output for nxm x nLoops
for i=1:N
T(:,:,i)=heat(x,y,VarToChange); % call the model with the other parameter(s)
VarToChange=VarToChange+dV; % increment the variable
end
You'll end up w/ 3D array with each plane one of the solutions for the particular VarToChange level.
You could, alternatively, use cell arrays and write the return values as
T{i} = heat(x,y,VarToChange);
and each would then be an nxm cell array.
댓글 수: 0
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!