Making the variable zero before going to the called function
8 views (last 30 days)
I have one complex problem, for me it is very complex.
I need to make a variable zero inside the if statements. but here if statements is a called function which contains three input arguments(Area, width, Diameter).
code like this:
% A is ara matrix
% D is diamter matrix
% W is width matrix.
Function L = Area_1(A, W,D)
if W == 0
L = .........
L = ......
Function L_A = Leitwert
[A,W,D] = Matrix_1;
% width matrix is like this W = [10 20 30 0];
% Diameter matrix is like this D = [ 5 5 5 0];
for i = 1:size(A,1)
for j = 1:size(A,2)
if M_1 == metal && M_2 == metal
L_A = Area_1(a,w,d);
elseif M_1 == metal && M_2 == air
L_B = .......
% The problem is how to make width zero inside the if statements even though it is a value.
For an example: i = 1, j = 1;
width = 10 Diameter = 5;
L_A(1,1) = ........ % (here second equation is sloved in the function Area_1 to give L_A)
Now I want L_A(1,2) now
but L_A (1,2) should be from the first equation of the function Area_1 even though Width(W) is a value.
Why? I can't make W(1,2) = 0 since L_B needs that width value if the condition is metal and air..
If the question is not clear still, kindly ask!
any suggestions and answered will be most welcomed!
James Browne on 12 Jun 2019
Edited: James Browne on 12 Jun 2019
I think I understand what your problem is and I think I may have a solution for you. Perhaps you should use a different variable in the function Area_1 to decide which equation to use, call it "eq" or something (name does not really matter, obviously). Then in the function Area_1 would look something like:
function L = Area_1(a, w,d)
% a = area
% w = width
% d = diameter
if eq == 0
L = a * d % when material and air are in series
L = a*d*w % when material and air are in parallel
Then all you have to do is set the variable "eq" to zero when it is appropriate to use equation 1 and set "eq" to 1 (or some other value other than zero) when it is appropriate to use equation 2. I do not really see a reason to use the variable "w" in the if statement which decides which equation to use, but maybe I am missing something?
If you do need to use "w" to decide which equation to use, you can always store the current value of "w" in a different variable, say "x", for example, then set "w" to zero to select the correct equation in the Area_1 function, once the Area_1 function completes its task, use the value stored in "x" to reset the current value of "w" to its original value. I can't quite tell from your code when materials are in seriese or parallel so I can't write the logic staement for you but if you wanted to be able to set "w" to zero and then reset it, it could be done in a way similar to the following code:
if ('logic statement for "w" needs to be zero')
x = w(i);
w(i) = 0;
L_A = Area_2( A(i,j),W(i),d(i));
w(i) = x;
I hope this at least points you in the right direction~