# Making the variable zero before going to the called function

5 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 = .........
else
L = ......
end
end
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 = .......
end
end
end
% 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!

Jan on 11 Jun 2019
The pseudo-code is not clear, but confusing. Please use standard Matlab syntax and post working code.
What is "Matrix_1"? Where do M1 and M2 come from? In
L_A = Area_1(a,w,d);
a, w, d are undefined. width does not occur anywhere, so what is the point of "make width zero"?
Okay,
I have four materials 1,2,3,4.
1,2,3 are three materials and 4 is air
1 and 4 are in series and 2 and 4 are in parallel 3 and 4 are in series,
1 and 2 in series, 2 and 3 in series
A is area matrix (4,4)
% D is diameter matrix (1,4)
% W is width matrix.(1,4)
Function L = Area_1(a, w,d)
% a = area
% w = width
% d = diameter
if w == 0
L = a * d % when material and air are in series
else
L = a*d*w % when material and air are in parallel
end
Function L = Area_2(a, w,d)
L = a*d*w % when material and material are in parallel %and in series
end
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];
% Area matrix is like this A = [ 0 47 0 23; 47 0 47 18; 0 47 0 23; 23 18 23 %0];
for i = 1:size(A,1)
for j = 1:size(A,2)
M_1 = Material(i); % called function to check whether i and j material %or air
M_2 = Material(j);
if M_1 == metal && M_2 == metal
L_A = Area_2( A(i,j),W(i),d(i));
disp(L_A);
elseif M_1 == metal && M_2 == air
L_B = Area_1(A(i,j),W(i),d(i));
disp(L_B);
elseif M_1 == air && M_2 == metal
L_B = 100;
end
end
end
Now the problem is when the i = material and j = air, L_B(1,4) or L_B(3,4) or L_B(2,4) slover goes to L_B = Area_1 (); when the slover goes into this function, always A(i,j), D(i) and W(i) will have the values, L_B will give the parallel equation solved value since W(i) will not be zero.
So here How to make W(i) is zero when the Material and air in series and L_B should be value of computed series equation, which menas L_B(1,4) or L_B(3,4) W(i) should be zero. Here we can't simple write if L_B(1,4) or L(3,4) which menas i == 1 && j== 4, W(i) == 0;
As I said earlier, the connection can be changed, may be for other model, 1 and 4 are in parallel, 4 may be material and 5 may be air then L_B(1,4) becomes L(1,5) in parallel.
Since code should be general, I can't make any changes in the Input matrices, because connection will be changed for the other models.
Here solver should know this is in series W(i) should be zero , when in parallel W(i) should not be zero.
I think this expplanation is enough.
if you have any suggestions, most welcomed.
Jan on 11 Jun 2019
I've formatted the text as text and the Matlab code as code to improve the readability. If you write "function" with lower case characters and apply a standard indentation (press Ctrl-a Ctrl-i in the editor), the code would be even nicer.
"when the i = material and j = air, L_B(1,4) or L_B(3,4) or L_B(2,4) slover goes to L_B = Area_1 ()" - I do not understand, what this means. L_B is set by L_B = Area_1, which replies a scalar. Then there is no L_B(1,4) or L_B(3, 4). I do not understand which "solver" is "going" to where.
Sorry, I do not get an idea of what you try to do.

James Browne on 12 Jun 2019
Edited: James Browne on 12 Jun 2019
Greetings,
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
else
L = a*d*w % when material and air are in parallel
end
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;
end
I hope this at least points you in the right direction~

R2014a

### Community Treasure Hunt

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

Start Hunting!