2

I was trying to solve Lorenz System in matlab by using the method of RK2 and RK4. I had a script for both of the methods, the problem now is how can converge the following

y(1) = @(t,y) 10*(y(2)-y(1));
y(2) = @(t,y) y(1)*(28-y(3))-y(2);
y(3) = @(t,y) y(1)*y(2)-8*y(3)/3;

into simply an column vector of y.

here is what i was hoping for and it never worked:

y = zeros(3,1);
y(1) = @(t,y) 10*(y(2)-y(1));
y(2) = @(t,y) y(1)*(28-y(3))-y(2);
y(3) = @(t,y) y(1)*y(2)-8*y(3)/3;

and following is my RK2 function. my RK4 is similar to this RK2, manybe this can help you understand why I really need an vector of functions.

function y = RK2(fcn,lrange,urange,step,init)
%fcn = vector of functions
%lrange = lower bound
%urange = upper bound
%step = number of steps
%init = initial value

row = size(fcn,1);
stepsize = (urange-lrange)/step;
y = zeros(row,step);
%initializing vector of y
y(:,1) = init;
%initial condition
t = zeros(1,step+1);
%initializing vector of t

if row ~= size(init,1)
    disp('number of functions and number of initial values do not match');
end

for n = 1:step

    t(n) = (n-1)*stepsize;
    t(step+1) = urange;
    y1 = stepsize.*fcn(t(n),y(:,n));
    y2 = stepsize.*fcn(t(n) + stepsize/2, y(:,n) + y1./2);
    y(:,n+1) = y(:,n) + y2;

end
beaker
  • 16,331
  • 3
  • 32
  • 49
Lechen Yuan
  • 23
  • 1
  • 1
  • 3

2 Answers2

3

Alternatively,

f = cell(3,1); % create a cell array

% initialize

    f{1} = @(t) t^2;
    f{2} = @(t) cos(2*t);
    f{3} = @(t) 4*(t^3);

% access properties

    size(f)(1); % access the number of functions
    f{1} % access the first function
    f{2}(17) % evaluate the second function at x = 17

A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data.

Documentation: https://www.mathworks.com/help/matlab/ref/cell.html

Alternatively, In Octave:

f = cell(3,1); # create a cell array

# initialize

    f(1) = @(t) t^2;
    f(2) = @(t) cos(2*t);
    f(3) = @(t) 4*(t^3);

# access properties

    size(f)(1); # access the number of functions
    f{1} # access the first function
    f{2}(17) # evaluate the second function at x = 17
  • You need to do either `f(1) = {@(t) t^2};` or `f{1} = @(t) t^2;`. Also, `#` is not valid syntax in MATLAB. – Cris Luengo Apr 01 '19 at 18:10
  • My apologies, I use Octave which is MATLAB for Linux and I hadn't thought there was a difference between that and MATLAB. In Octave # works for a comment Should this be removed as an answer? – Gabrielle Stewart Apr 02 '19 at 21:55
  • No, don't delete it, but do state that it's a solution for Octave. (BTW: MATLAB is supported on Linux, and Octave works just as well on MacOS and Windows and many other OSes, I don't think "MATLAB for Linux" is a correct description of Octave. Octave and MATLAB have a similar syntax, largely compatible, but not identical. There are many differences.) – Cris Luengo Apr 02 '19 at 22:10
2

This should work, just make the function output a vector:

y = @(t,y) [10*(y(2)-y(1)), y(1)*(28-y(3))-y(2), y(1)*y(2)-8*y(3)/3];
y(1,[1;2;3])
size(y(1,[1;2;3]))
David
  • 8,449
  • 1
  • 22
  • 32
  • hello, thanks for answering my question. I just tried what you answered, it seems it did make my functions into one vector. the problem is when I try to count the number of rows of this vector it either shoot out 1 or "Dimension argument must be a positive integer scalar within indexing range." do you know how to fix this? or how to count the number of rows in this vector if i made it a row vector. Thank you so much! – Lechen Yuan Dec 08 '14 at 01:11
  • Try replacing the semicolons in the array with commas to make a row vector not a column vector too. – David Dec 08 '14 at 01:14
  • I tried both ways you provided and neither worked. somehow I just cannot count the number of arguments of that array of functions. – Lechen Yuan Dec 08 '14 at 01:24
  • even if I used unmel(y), it always shoot out 1 instead of 3. – Lechen Yuan Dec 08 '14 at 01:29
  • That's correct. `y` is a function, not an array. It's only when you give `y` inputs and evaluate the result that you get an array. – David Dec 08 '14 at 01:30
  • oh, well, my problem is im trying to make a system of three equations into an array or a vector, and I need to count how many functions i have in that array and vector in RK2. I have searched all over the web and just could not find an answer. – Lechen Yuan Dec 08 '14 at 01:34
  • if that is too complicated, I will try to figure it out through other ways. Thank you a lot anyway! – Lechen Yuan Dec 08 '14 at 01:34
  • Presumably you have one equation for element of the result vector, so just use that, or count the number of initial conditions (one for each equation) you have. – David Dec 08 '14 at 01:37
  • I kinda see my problems now. I just decided to redo my RK2, because I made it the way too complicated. Thank you a lot! without you I couldnt get this far. – Lechen Yuan Dec 08 '14 at 02:02