function where input is also output

Is it possible to have a function where the argument is also calculated in the same function?

4 commentaires

Mathieu NOE
Mathieu NOE le 22 Mai 2024
I don't think so
for me a function has always distinct input and output arguments - what are you trying to achieve ?
Marina
Marina le 22 Mai 2024
I'm trying to calculate a temperature using a flow. Once the temperature is found, I'd like to recalculate this flow to use it for the next calculation... and so on.
ok then you can do it by iterations and hopefully it should converge , like
% start with initial values of flow and temperature
temp = ...
flow = ...
tol = 1e-3; % tolerance on result - whatever you want
while error > tol % repeat the loop until results have converged
temp_new = function1(flow); % update temperature (with function1)
flow_new = function2(temp_new); % update flow (with function2)
% compute error
error = abs(flow_new - flow) + abs(temp_new - temp); % other error metrics are possible, this is just one example
% update flow / temp
flow = flow_new;
temp = temp_new;
end
Marina
Marina le 23 Mai 2024
I will try . Thanks

Connectez-vous pour commenter.

Réponses (2)

You can certainly return the input as an output if you want to.
A simple example —
x = 42;
[result_val, arg_val] = square_arg(x)
result_val = 1764
arg_val = 42
function [result_val, arg_val] = square_arg(x)
arg_val = x;
result_val = x.^2;
end
.

2 commentaires

Marina
Marina le 22 Mai 2024
Thanks . I will try something like that
Star Strider
Star Strider le 22 Mai 2024
My pleasure!

Connectez-vous pour commenter.

Kunal Kandhari
Kunal Kandhari le 22 Mai 2024

0 votes

Hi Marina,
I understand that you are seeking to utilize pass-by-reference to modify the input argument passed into a function. The answer to the question depends on whether the input is a handle object or a value object.
The following MATLAB Answers post will explain how to achieve that:

Catégories

En savoir plus sur 2-D and 3-D Plots dans Centre d'aide et File Exchange

Question posée :

le 22 Mai 2024

Commenté :

le 23 Mai 2024

Community Treasure Hunt

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

Start Hunting!

Translated by