function where input is also output

조회 수: 4 (최근 30일)
Marina
Marina 2024년 5월 22일
댓글: Marina 2024년 5월 23일
Is it possible to have a function where the argument is also calculated in the same function?
  댓글 수: 4
Mathieu NOE
Mathieu NOE 2024년 5월 22일
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 2024년 5월 23일
I will try . Thanks

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

답변 (2개)

Star Strider
Star Strider 2024년 5월 22일
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
Marina
Marina 2024년 5월 22일
Thanks . I will try something like that
Star Strider
Star Strider 2024년 5월 22일
My pleasure!

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


Kunal Kandhari
Kunal Kandhari 2024년 5월 22일
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:
  댓글 수: 1
Marina
Marina 2024년 5월 22일
Thanks you. I'll watch it right now

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

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by