How to run two independent functions simultaneously?

Hi.I have two functions for creating numbers.lu and lu1(similar to lu).lu 's out put is matrix with 4 columns (rows depend on inputs (same for lu1) )and i have dual core CPU.(numbers in each function is related, but two functions are Independent).
function Y=lu(a,b,c,d,e,k)
[T,Y]=ode45(@rigid,[0 e],[a b c d]);
function dy=rigid(t,y) %#ok<INUSL>
dy=zeros(4,1);
dy(1)=36*(y(2)-y(1))+y(4);
dy(2)=-(y(1)*y(3))+20*y(2);
dy(3)=y(1)*y(2)-3*y(3);
dy(4)=y(1)*y(3)+k*y(4);
end
end
I want to using parallel computing for my two functions by using this code
matlabpool ('open',2);
parfor i = 1:2
if i == 1
xc1=lu(0.10001,0.8,12,0.2,1138,1.2);
else
xc2=lu1(3,0.40001,9,0.7,1010,3.25)
end
in normal code xc1 is matrix with this size[91713 4](same for xc2). is it true way for parallel computing for two functions ? Importantly,how can I using xc1 and xc2 in other parts of my code?

댓글 수: 1

lu() is the name of a pre-existing MATLAB function. It might be wise to choose a different name.

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

 채택된 답변

Matt J
Matt J 2013년 9월 15일
편집: Matt J 2013년 9월 15일
If I were to do this, I would probably do it as below.
funcs={@lu,@lu1};
arguments={0.10001,0.8,12,0.2,1138,1.2;...
3,0.40001,9,0.7,1010,3.25};
solutions=cell(1,2);
parfor i = 1:2
solutions{i}=funcs{i}(arguments{i,:});
end

댓글 수: 3

sa za
sa za 2013년 9월 15일
Thank you.
Hello @Matt J
I tried to test with my really simple code as below, but the error is "Too many output arguments."
function func1(x,y)
plot(x, y), xlabel('x'), ylabel('Sin(x)'), title('Sin(x) Graph'),
grid on, axis equal
end
function func2(x,y)
plot(x, y), xlabel('x'), ylabel('Cos(x)'), title('Cos(x) Graph'),
grid on, axis equal
end
the main is
x = 0:0.01:10;
y1 = sin(x);
y2 = cos(x);
funcs = {@func1, @func2} ; % let fun1, fun2 be two functions
arguments = {x y1;x y2} ; % write the inputs of each function
solutions = cell(1,2); % initialize the solution
% use of parfor
parfor ii = 1:2
solutions{ii}=funcs{ii}(arguments{ii,:});
end
Please help!!!
Your functions do not return any values. You cannot assign "no values returned" to an output locaiton.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

질문:

2013년 9월 15일

댓글:

2019년 10월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by