How to run two independent functions simultaneously?

8 次查看(过去 30 天)
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 个评论
Matt J
Matt J 2013-9-15
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 个评论
NGOC TAM LAM
NGOC TAM LAM 2019-10-4
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!!!
Walter Roberson
Walter Roberson 2019-10-4
Your functions do not return any values. You cannot assign "no values returned" to an output locaiton.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by