MATLAB Short Course 4: MATLAB functions and solving differential equations

Contents

get the complex square z+iw = (x+iy)^2 =x^2-y^2+2xyi

%function [z,w] = myfun3(x,y)

z + iw = (x+iy)^2 = x^2-y^2 + 2ixy

%z = x^2 - y^2;
%w = 2*x*y;
[z, w] = myfun3(1,1)
z =
     0
w =
     2

only the first output is displayed

myfun3(1,1)  % only z is displayed
ans =
     0

Solve the ODE y' = -y^3+sin(t)

% define it as a simple anonymous function
myode1 = @(t,y) -y^3+sin(t);
% call the generic solver "ode45"
[t,y] = ode45(myode1,[0,3],-1);
plot(t,y)

Solve the more complicated Lorenz ODEs

[t,Y] = ode45(@(t,y)lorenzode(t,y),[0 50],[1 0 0]);
% Y  is a large matrix with three columns (three variables)
plot3(Y(:,1),Y(:,2),Y(:,3))