How to Use Symbolic Functions in For Loops

1 vue (au cours des 30 derniers jours)
Tom Keaton
Tom Keaton le 7 Oct 2018
Modifié(e) : Tom Keaton le 31 Oct 2018
I would like to use a symbolic function in a for loop, however I spent hours reading the resources about symbolic functions and for loops and am still unable to figure out how I can go about doing this. My goal is to define the initial conditions of a particle (Position [x0 y0 z0] and Velocity [vx0 vy0 vz0]) and based on those, have the for loop calculate the position and velocity of the particle at previously defined time steps. This is all I have gotten started with on code:
syms x(t) y(t) z(t) vx(t) vy(t) vz(t)
%Untis of cm/s
%Initial Conditions
s0 = [5 5 5];
v0 = [-5E+8 0. -5E+8];
x(0) = s0(1);
y(0) = s0(2);
z(0) = s0(3);
vx(0) = v0(1);
vy(0) = v0(2);
vz(0) = v0(3);
for t = 0:1E-10:10E-8 %Time step and interval
The reason I want to use a symbolic function for this is because position and velocity depend on time and I also want to add a function that randomly decides to change the velocity vector at a given time within the for loop. If anyone needs any more contextual information I will provide it as swiftly as possible, thanks.

Réponse acceptée

Walter Roberson
Walter Roberson le 7 Oct 2018
Use a cell array into which you put symbolic time tests and corresponding output formulae. Then piecewise(TheCell{:}) to construct the overall formulae.
  3 commentaires
Walter Roberson
Walter Roberson le 11 Oct 2018
"Wouldn't that mean I would need to define each time step individually?"
Each different condition would have to be defined, which does not necessarily mean each timestep. If no velocity change happens until time t = 8.3 then you do not need entries for t = 0.1, 0.2, 0.3, ... 8.2 for example.
"Also what about if the position and velocity are changing dynamically, how could that be integrated into the for loop?"
You alter or add the appropriate entry to the cell array and piecewise() to construct the updated version.
"I want the for loop to take into account a random change in the velocity and update position each time the loop runs."
The above handles that situation.
You need to understand that the situation you have defined is one in which you are randomly changing the model of how the particle is moving. For example you might be modeling ants and wanting to study and randomly change their trajectories while you calculate integrals or statistical measures in closed form (the reason you need formulas)
If you were just doing Random Walk or Monte Carlo simulations, then you would not want symbolic form at all: you would simply have arrays of numeric current positions and velocities for particles, and you would alter the velocities from time to time and use standard update formula of new_position = position + velocity * timestep (assuming vector valued positions and velocity.)
Tom Keaton
Tom Keaton le 31 Oct 2018
Modifié(e) : Tom Keaton le 31 Oct 2018
Got it, thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by