MATLAB Answers

How to differentiate condition in if statements

5 views (last 30 days)
Hey all,
I know what I am asking is wrong, but I have to slove it at any cost.
I have sOme materials .1 2 3 4 5 6 7 8 9 10;
Now 1 and 2 are in series 1--2
2 and 3 are in series 2--3
3 and 4 are in series 3 --4
1 and 5 are in parallel =
8 and 5 are in series 8-5
8 and 6 are in parallel =
6 and 7 are in series 6-7
5 and 9 are in Series 5 - 9
9 and 10 are in series 9 -10
9 are 2 are in parallel =
10 and 3 are in parallel =
Now I need to build separate function file to let the slover knows which one is in parallel or series with other element.
My code is like this:
classdef Connection
enumeration
Series, Parallel
end
end
function [C] = find_Connection(i)
[Lambda, A, B, D, M, A1,M_S, M_P] = Matrix_surendra13; %% this is the input file which %contains the input arguments.
C = 0;
% M_S is a row vector which contains materials in Series . M_S = [ 1 2 3 4 5 8 6 7 9 10];
%M_P is a row vector which contains materials in parallel M_P = [ 1 5 7 9 10];
if (find(M_S == i))
C = Connection.Series;
elseif (find(M_P == i))
C = Connection.Parallel;
end
function Slover
[Lambda, A, B, D, M, A1,M_S, M_P] = Matrix_surendra13; % ignore this called function it is just an %input argument file
for i = 1:Size(A,1) % A from the input argument file square matrix always
for j = 1:size(A,2)
C1 = find_Connection(i);
C2 = find_Connection(j);
if (C1 == Connection.Series && C2 == Connection.Series)
LA = A(i,j)*B(i)*D(j)*Lambda(j);
elseif (C1 ==Connection.Parallel && C2 == Connection.Parallel)
LB = A(i,j)*Lambda(i)*Lambda(j);
end
So the problem is each material has two possiblities either Series or Parallel with others. If I use my code it doesn't work.
For an example,
i = 1; j = 1;
Material 1 is in Series with 2 and in Parallel with 5, when the solver enter the function file find_Connection(i) it will automatically take Series statement since first condition will be fulfilled. This is fine for 1 and 2. LA will be computed.
But when i = 1 and j = 5; in parallel
slover sholud take LB, but it will compute LB bcoz both i = 1 and i = 5 are there in series vector file as well as in parallel vector file, so first condtion in the find_Connection file will be fulfuilled, so solver will not check second one.
I think that I gave the proper explanation.
Kindly as for the more detail if it is not clear.
any suggestions and answers are most welcomed.
Thanks in Advance.

  3 Comments

Guillaume
Guillaume on 13 Jun 2019
If I use my code it doesn't work.
Not surprised by that, I don't even understand what your find_connection function is supposed to do.
I have no idea what it means for materials to be in parallel or in series but I don't think that's important. What I don't understand at all is that in your description the parallel or serial property is a property of a pair of elements (e.g. 1 and 2, or 1 and 5) whereas you're using a single vector to describe the property. How does that work? I certainly don't see how your M_P vector describes that 8 and 6 are in parallel.
In my opinion, you would be better off using a categorical array to store the parallel or serial state rather than an enumeration class. I find the implementation of enumerations in matlab very awkward.
If I understand what you're trying to do correctly, note that you shouldn't need to use loops at all. Also note that matlab is not C, you don't need () around an if condition.
James Browne
James Browne on 15 Jun 2019
I agree with that strategy, even a simple cell array could do the trick, like:
materialCombos = {...
'1' '2' 'series'
'2' '3' 'series'};
Then the following parameters are easily accesed:
material1 = materialCombos(i,1);
material2 = materialCombos(i,2);
relationship = materialCombos(i,3);
Then you just sent material1, material2 and relationship to your solver~

Sign in to comment.

Answers (0)

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!