How to take precalculated variables in a function, without defining the variables within said function?

8 views (last 30 days)
my code looks like this
function D = down(A, B, C, n)
A = (dependent on other variables);
B = (dependent on other variables);
C = (dependent on other variables);
n=20;
D = sqrt(A)/2;
for i = 1:n
DA = (D-A/4./sqrt(B^2+(C+D)*(C+D)))/(1+(C+D)*A/4./(B^2+(C+D)*(C+D))^1.5);
D = D - DA;
end
end
A, B, and C are calculated in the script prior to the function being defined, and therefore they depend on other variables. However, if I were to include every variable that A, B and C depend on within the function, then I would basically have to include my entire script as the variables that A depends on, depend on other variables themselves and so on.
I want to be able to take the values that are precalculated for A, B and C (before the function is active), into the function to calculate the new value for D. Whenever I try to include the equations for A to C, I'm met with the error of undefined variables. Is there a way to do this?
I can not set A etc. to absolute values either, as they will change as I progress through a loop of mission legs. I hope I've been clear, thanks!
  7 Comments
Stephen23
Stephen23 on 12 Apr 2021
Edited: Stephen23 on 12 Apr 2021
"I want to know what I should write for A, B and C within the function down, so that they take the precalculated values before the function is called?"
Much less than you are doing now. You are overthinking the situation.
"I think my problem is occuring because I don't know how to write A to C within the function."
No. There are two problems, neither of them are "how to write A to C within the function":
  1. you are not calling the function with any inputs (although the intent is that it has three inputs).
  2. you are overwriting (and hence discarding) all of the input arguments within the function.
function D = down(A, B, C)
A = % get rid of this line. This is hindering you.
B = % get rid of this line. This is hindering you.
C = % get rid of this line. This is hindering you.
Call the function with all of its required input arguments:
down(A,B,C)
How to call functions with input arguments is shown here:

Sign in to comment.

Accepted Answer

Jan
Jan on 12 Apr 2021
Edited: Jan on 12 Apr 2021
You have defined the function down() with input arguments. Then use them:
A = T/(0.5*R*V^2*AR);
if(F~= 0);
M = F/V;
B = M*cos(G);
C = M*sin(G);
down(A, B, C);
% ^ ^ ^
...
function D = down(A, B, C)
D = sqrt(A)/2;
for i = 1:20
DA = (D - A / 4 ./ sqrt(B^2 + (C + D) * (C + D))) /...
(1 + (C + D) * A / 4 ./ (B^2 + (C + D) * (C + D))^1.5);
D = D - DA;
end
end
You do the same, what you call sqrt with an input argument:
A = 17
sqrt() % Nope
sqrt(A) % Yes!
This is a duplicate of the explanations DGM has give already.
  3 Comments
Jan
Jan on 12 Apr 2021
You are welcome. There is no reason for apologies, because questions about Matlab are the topic of this forum. Of course any problem is trivial as soon at it is solved :-)

Sign in to comment.

More Answers (0)

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!