ode45 code issue solving for polar coordinates
Show older comments
Okay so I trying to get ode45 to work. To do this I established a function V which represents the 3 velocities of a fluid. I establish all the intial conditions in the function as well as the varialbe. I want the ode45 function to output the 3 variables (r,T,z) Eventually I am going to use pol2cart to convert and plot the function in the x,y,z plan. Can anyone assist in pointing out were I am going wrong.
b = 2;
aspect = 0.5;
c = 3;
a = 2;
r = 2;
T = 0.1;
z = 5;
t = 0;
n = 50000;
Init_cond = 1;
tspan = [0 n];
% The values above are not the final values I am currently reseaching
% What they represent and trying to determine an appropate model
% They will be updated soon
[r,T,z] = ode45(@odeFun, tspan, Init_cond);
[x,y,z] = pol2cart(r,T,z);
plot3(x,y,z);
% using the matlab funciton ode45 I want it to output the variables r,T,z
% input function is odeFun which is equal to the velocity functions
% origianlly provided
% Initial condition is just so for all variables so r, T and z = 0 when
% time equals zero
function V = odeFun(r,T,z)
b = 2;
aspect = 0.5;
c = 3;
a = 2;
r(1) = 0; % initial conditions
T(1) = 0; % initial conditions
z(1) = 0; % initial conditions
V = zeros(3,2,1);
V(1) = @(r,T,z) r*(z-b)/aspect; % Velocity in the radius
V(2) = @(r,T,z) c; % Velocity in the Theta
V(3) = @(r,T,z) z*(1-a*r); % Velocity in the z direction
end
1 Comment
Philip Binaco
on 10 Oct 2021
Edited: Walter Roberson
on 10 Oct 2021
Accepted Answer
More Answers (1)
Walter Roberson
on 10 Oct 2021
dVdt(3) = @(r,T,z) z*(1-a*r); % Velocity in the z direction
That attempts to create an anonymous function and store the function handle as the third element of dVdt . However, it is not permitted to create ()-indexed arrays of function handles, so if you needed to create an array of function handles you would need
dVdt = cell(3,1);
dVdt{1} = @(r,T,z) r*(z-b)/aspect; % Velocity in the radius
dVdt{2} = @(r,T,z) c; % Velocity in the Theta
dVdt{3} = @(r,T,z) z*(1-a*r); % Velocity in the z direction
... however, what you return from the ode function must be strictly datatype single() or datatype double() .
What you return from the ode function must be a column vector that has the same number of elements as your initial condition. Your initial condition is a scalar, so unless you change your initial conditions, you would need to return a scalar.
3 Comments
Walter Roberson
on 10 Oct 2021
I want the ode45 function to output the 3 variables (r,T,z)
Your approach is wrong. You need to bundle the three different variables into a single vector, and pass in a vector of three initial conditions, and your ode function needs to return a vector of derivatives the same size as your initial conditions.
r = 2;
T = 0.1;
z = 5;
Are those your initial conditions for r, T, z?
r(1) = 0; % initial conditions
T(1) = 0; % initial conditions
z(1) = 0; % initial conditions
or are those your initial conditions for r, T, z ?
Philip Binaco
on 10 Oct 2021
Walter Roberson
on 10 Oct 2021
As I wrote above,
... however, what you return from the ode function must be strictly datatype single() or datatype double() .
Returning a cell array of anonymous functions is not permitted .
The ode*() routines do numeric integration. They figure out a step size and they propose a series of boundary conditions based upon continuity models, and they test to see whether the proposed positions give values within tolerance for the model. If the proposal is accepted, then they take the returned numeric values (which are instantaneous derivatives), multiply them by the step size, and add those to the previous boundary conditions to get new boundary conditions.
The routines do not expect anonymous functions to be returned, and will not call any returned anonymous functions to determine numeric values: the routines test whether isfloat() the returned values, and if not then error() .
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!