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

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

3 Comments

You need to do either f(1) = {@(t) t^2}; or f{1} = @(t) t^2;. Also, # is not valid syntax in MATLAB.
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?
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.)
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]))

9 Comments

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!
Try replacing the semicolons in the array with commas to make a row vector not a column vector too.
I tried both ways you provided and neither worked. somehow I just cannot count the number of arguments of that array of functions.
even if I used unmel(y), it always shoot out 1 instead of 3.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.