How to call a multi output function without repeating the run

Dear all,
I am having a challenge, which I think, should be possible to overcome, but how to go about it has been challenging for me, I have a function with multiple output in this case 3 output, and I am calling all three output for further use. The issue now is how do I call the three output without repeating the entire calculation in the process. As I have observed that for the 3 times I called it, it runs afresh all 3 times. In the first place calling it once is time consuming how more calling it 3 times, whic makes the process computational expensive to achieve.
Thanks
Kind regards
%The function were all the three output from the multi-output function are to be %inserted for further use.
function [c, ceq] = nonconst(x)
c = total_value(x)-u*v;... ` `%first output from the multi-output %function
u*energy_value(x);... %2nd output from the multi-output %function
v*Delta_value(x); %Third output from the multi-output %function
ceq = [];
end
%The function to call the first output from the digital function
function D_total = total_value(x)
D_total = digital(x) % ca
end
%The function to call the 2nd output from the digital function
function D_value = Delta_value(x)
[~, D_value, ~] = digital(x)
end
%The function to call the third output from the digital function
function D_energy = energy_value(x)
[~, ~, D_energy] = digital(x)
end
%This is a multi-output function comprises of 3 output which are D_total,
% D_value and D_energy
function [D_total, D_value, D_energy] = digital(x)
;;;;;;;;;;;; %The conetent of the function has been cleared
;;;;;;;;;;;
;;;;;;;;;;
end

1 Comment

This looks to be very similar or the same as the question you were asking here where you received some explanation and advice. Why did you start another question? Clarifying questions and suggestions made in one of the discussions won't be visible to participants in the other.

Sign in to comment.

 Accepted Answer

function [c, ceq] = nonconst(x)
[total_value, Delta_value, energy_value] = digital(x);
c = total_value-u*v;... ` `%first nonlinear constrain
u*energy_value;... %2nd Nonlinear constrain
v*Delta_value;
ceq = [];
end

6 Comments

Dear Fabio
I have another concern, now I wish to paint a scenrio were 2 of the 3 output functions, are called in one function as stated below and 1 is called in another function. Now my question is I want it to run once instead of twice. Is there a way I can sort of arrange that.
Thanks
Joshua
eg.
function [c, ceq] = nonconst(x)
u = 20;
v = 40;
[total_value, ~, energy_value] = digital(x);
c = total_value-u*v;... ` `%first constrain
u*energy_value; %2nd constrain
ceq = [];
end
%let say I wish to make this the fitness function which is derived as one of the output from the 3 output.
function Delta_value = parcel(x)
[~, Delta_value, ~] = digital(x)
end
It is not clear where you want to have the Delta_value in your code: where is the function calling parcel(x)?
Hi Fabio
sorry I have not been able to respond to your messenge ealier
I have a scenerio playing in my head which i want to achieve, as I have explained earlier
The Challenge I am having is I wish to run both nonconst and parcel function one after the other however, I don't need the digital function to be run twice, as demostrated below. I wish the second function should be able to benefit from the first run.
Thanks
Joshua
eg.
function [c, ceq] = nonconst(x)
u = 20;
v = 40;
[total_value, Delta_value, energy_value] = digital(x);
c = total_value-u*v;... ` `%first constrain
u*energy_value; %2nd constrain
ceq = [];
end
function lineweight = parcel(x)
[total_value, Delta_value, energy_value] = digital(x);
p_s = 7850; %Density of steel Kg/m^3
g = 9.812; %Acceleration due to gravitation
k = 0.72
lineweight(1) = p_s*g*(pi/4)*(x(1)+x(2))*((x(3))^2-0.254^2);
lineweight(2) = k*Delta_value
T2 = table(x(1), x(2), x(3), lineweight(1), lineweight(2))
end
you can declare the outputs of your digital function as global. See
doc global
Hi Fabio
I taught about that but was a bit uncertain.
Thanks for the suggestion .
kind regards
Joshua
For such trivial code as this you should not get into the bad habit of using global variables.
Passing data as input/output arguments is neater and much more efficient. Passing data as input/output arguments is what the MATLAB documentation (and all experienced MATLAB users) recommend:
Another very efficient option is to use nested functions:

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!