Pick out rows one by one from a vector to use as initial values in differential equation?

1 view (last 30 days)
Hi everyone!
I just finished a school project where I had use a while loop to solve a second order differential equation. I just needed a vector with four initial values in it and then solve the equation with Runge Kutta which I did with a while loop.
While this is not included in the project, I just wonder: assume I have a vector with 4 columns and say 100 or 500 rows, where each column represents one initial value. I want to use the first row as starting values for the runge kutta while loop. After the lopp has completed I want to run the while loop again but with the second row from the vector as starting values. After that I want to runt the while loop with the third row from the vector as starting values!
You get the idea?
My question is how do I achieve this? I assume I should use a for loop which I have tried without success. I hope you understand the question, I am fairly new to Matlab so I have a hard time talking in "matlab" language :P
Best regards and thanks for your help!

Answers (1)

James Tursa
James Tursa on 22 Oct 2014
Edited: James Tursa on 22 Oct 2014
Do you mean something like this?
initial_conditions_matrix = (a 100 x 4 matrix)
m = size(initial_conditions,1);
for k=1:m
initial_conditions = initial_conditions_matrix(k,:);
% Then your while loop for solving DE using initial_conditions vector
end
  2 Comments
Michael B
Michael B on 22 Oct 2014
Thanks for your quick answer!
I think that's what I meant! If I understood what you wrote correct, your for loop will run the while loop once for every row in the initial_condidions matrix and use the columns as initial values in the while loop?
Also, in the second row, what does the 1 mean in "m=size(initial_conditions, 1);?
Best regards!
James Tursa
James Tursa on 22 Oct 2014
Edited: James Tursa on 22 Oct 2014
The m calculation is simply determining the number of rows in your initial condition matrix. Rather than hard-code the number of rows into the for loop, calculating m at run-time makes the code more robust so that you can change the size of your initial conditions matrix at any time and not have to worry about changing the indexing in your for loop downstream.

Sign in to comment.

Categories

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

Community Treasure Hunt

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

Start Hunting!