Clear Filters
Clear Filters

How to run two independent functions simultaneously?

8 views (last 30 days)
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 Comment
Matt J
Matt J on 15 Sep 2013
lu() is the name of a pre-existing MATLAB function. It might be wise to choose a different name.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 15 Sep 2013
Edited: Matt J on 15 Sep 2013
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 Comments
NGOC TAM LAM
NGOC TAM LAM on 4 Oct 2019
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 on 4 Oct 2019
Your functions do not return any values. You cannot assign "no values returned" to an output locaiton.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!