For Loop Indices Manipulation
Afficher commentaires plus anciens
Hi all. I have a function file which has a for loop which, depending on the conditions, returns certain equations that should end up creating a vector of differential equations, of number x that is determined by user input, that would be solved with 'ode45' in the main m-file. The issue is that the loop must start at 3 in order to have real positive integers.
function [ DeltaO2Concentration ] = O2Solver_beta(t,O2Conc,c,O2Leak_term,nodes_num,slt_nodes2,last_node)
%UNTITLED Test function used to solve 'first attempt' code.
% System of ODE diffusion equations that will be solved using 'ode45'
% function. Inputs are constant composed of universal and calculated
% constants, time (independent variable), O2Conc (dependent variable),
% O2Leak which is a separate term or something.
%VECTOR LENGTH WILL NEED TO VARY ACCORDING TO USER INPUT%
% DeltaO2Concentration = [0;
% c*t*(O2Conc(1)-O2Conc(2)+O2Conc(3)-O2Conc(2));
% c*t*(O2Conc(2)-O2Conc(3)+O2Conc(4)-O2Conc(3));
% c*t*(O2Conc(3)-O2Conc(4)+O2Conc(5)-O2Conc(4))%+O2Leak;
% c*t*(O2Conc(4)-O2Conc(5)+O2Conc(6)-O2Conc(5));
% c*t*(O2Conc(5)-O2Conc(6)+O2Conc(7)-O2Conc(6));
% c*t*(O2Conc(6)-O2Conc(7)+O2Conc(8)-O2Conc(7))+O2Leak;
% c*t*(O2Conc(7)-O2Conc(8)+O2Conc(9)-O2Conc(8))+O2Leak;
% c*t*(O2Conc(8)-O2Conc(9)+O2Conc(10)-O2Conc(9))+O2Leak;
% c*t*(O2Conc(9)-O2Conc(10)+O2Conc(11)-O2Conc(10));
% c*t*(O2Conc(10)-O2Conc(11)+O2Conc(12)-O2Conc(11));
% c*t*(O2Conc(11)-O2Conc(12)+O2Conc(13)-O2Conc(12));
% c*t*(O2Conc(12)-O2Conc(13)+O2Conc(14)-O2Conc(13))%+O2Leak;
% c*t*(O2Conc(13)-O2Conc(14)+O2Conc(15)-O2Conc(14));
% c*t*(O2Conc(14)-O2Conc(15)+O2Conc(16)-O2Conc(15))%+O2Leak;
% c*t*(O2Conc(15)-O2Conc(16)+O2Conc(17)-O2Conc(16));
% c*t*(O2Conc(16)-O2Conc(17)+O2Conc(18)-O2Conc(17));
% c*t*(O2Conc(17)-O2Conc(18)+O2Conc(19)-O2Conc(18))%+O2Leak;
% c*t*(O2Conc(18)-O2Conc(19)+O2Conc(20)-O2Conc(19));
% 0];
terminate= nodes_num;
DeltaO2Concentration= zeros(1,terminate);
cnt= 1;
for i= 3:length(DeltaO2Concentration)
if i==3 && ismember(i,slt_nodes2)
DeltaO2Concentration(cnt) = c*t*(O2Conc(i-2)-O2Conc(i-1))+O2Leak_term; %first node, leakage
elseif i==3
DeltaO2Concentration(cnt) = c*t*(O2Conc(i-2)-O2Conc(i-1)); %first node, NO leakage
elseif i==last_node && ismember(i,slt_nodes2)
DeltaO2Concentration(cnt) = c*t*(O2Conc(i)-O2Conc(i-1))+O2Leak_term; %last node, leakage
elseif i==last_node
DeltaO2Concentration(cnt) = c*t*(O2Conc(i)-O2Conc(i-1)); %last node, NO leakage
elseif ismember(i,slt_nodes2)
DeltaO2Concentration(cnt) = c*t*(O2Conc(i-2)-O2Conc(i-1)+O2Conc(i)-O2Conc(i-1))+O2Leak_term; %other nodes, leakage
else
DeltaO2Concentration(cnt) = c*t*(O2Conc(i-2)-O2Conc(i-1)+O2Conc(i)-O2Conc(i-1)); %other nodes, NO leakage
end
cnt= cnt+1;
end
DeltaO2Concentration= DeltaO2Concentration.'; %column vector for ODE45
nodes_num: number of nodes user inputs, which translates to # of ODE equations.
slt_nodes2: user input selection of specific nodes that will include the 'O2Leak_term' in equation.
The commented out stuff in the beginning is just a general template that I use to create the loop.
If I run this as it is, the 'DeltaO2Concentration' vector is the correct length, but I feel like Matlab just puts in zero vectors or something at the end because I think it should return a vector two less than the desired, but I'm not sure how to interpret it or fix it to where I get the correct output. Any help in appreciated.
13 commentaires
Adam
le 27 Juin 2017
terminate= nodes_num;
DeltaO2Concentration= zeros(1,terminate);
is what tells Matlab exactly what to put in the vector initially and that will remain in those elements you do not overwrite. If you simply want to have two fewer elements then just make the vector shorter by 2 and update your indexing accordingly.
for i= 3:( length(DeltaO2Concentration) + 2 )
would be one of numerous ways to achieve this.
Areeb Hussain
le 27 Juin 2017
Adam
le 27 Juin 2017
Well, I guess you could just truncate the vector afterwards if that is easier. Since you are indexing into it with something that should reach 2 less than the size of the vector when the loop terminates it should never write to the last two elements of your vector so just
DeltaO2Concentration( end-1:end ) = [];
should do the trick equally.
I would probably index into that vector using i-2 personally, or at the very least set
cnt = i-2;
in the loop rather than incrementing cnt, but it should all amount to the same thing, just preference
dpb
le 27 Juin 2017
Just how many ODEs do you have? The derivative vector must be of that length.
We can certainly clean up the code as written significantly, but in reading your description I'm just not following the actual problem definition to know which is the right solution.
Steven Lord
le 27 Juin 2017
Areeb Hussain
le 28 Juin 2017
Adam
le 28 Juin 2017
Well, I mean trim them before they get fed in, straight after your for loop.
Areeb Hussain
le 28 Juin 2017
Areeb Hussain
le 28 Juin 2017
dpb
le 28 Juin 2017
OK, so now we know you do want N ODEs for the input number.
What you've got builds 20 but two are empty which may/may not be what the actual ODE system needs.
What is RHS for the first entries supposed to be?
If you had a smaller system of say five, show us the actual ODEs to be solved to really nail down how to write this correctly for the system.
Areeb Hussain
le 28 Juin 2017
Modifié(e) : Areeb Hussain
le 28 Juin 2017
Areeb Hussain
le 28 Juin 2017
Guillaume
le 28 Juin 2017
Coming rather late in the conversation and not having much to say to answer the actual problem, I'll just comment on this:
terminate= nodes_num;
DeltaO2Concentration= zeros(1,terminate);
for i= 3:length(DeltaO2Concentration)
What's wrong with:
DeltaO2Concentration= zeros(1, nodes_num);
for i = 3:nodes_num
Why rename the variable and make the reader do extra mental work in order to understand the bounds of the loop?
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!