MATLAB: I am not able to figure out what the problem is

It says "The expression to the left of the equals sign is not a valid target for an assignment." Line 20 Column 19.
The Main Script:
y_initial = 0; %initial Position
yd_initial = 1; %Initial Velocity
h = 0.4; %Increments
tspan = [0,40]; %Range of value of t.
y0 = zeros(2); %initializing y0
y0(1) = y_initial; %y0(1) is initial position
y0(2) = yd_initial; %y0(2) is initial velocity
%yd^2 = -sin(y) + cos(3t)
%yd(1) = y(2)
%yd(2) = cos(3t) – sin(y(1))
%y(2) = y_dot _________ Velocity
%y(1) = y ___________Position
yd = @(t,y) [y(2); cos(3*t) – sin(y(1));
%Using Runge Kutta
[t,y_runge_kutta] = rk4(yd,tspan,y0,h);
%Using Heun Method
[t,y_heun] = heun(yd,tspan,y0,h);
____________________________________________________________________________
rk4 Function:
function [ t,y ] = rk4( yd,tspan,y0,h )
%This function solves differential equations using Runga-Kutta Method
%yd is the system of two differential equations of the form:
%dy/dt = f(t,y) (= dy)
t = tspan(1):h:tspan(2); %t is vector containing all the t values in increment of h.
N = length(t); %number of entries of t
y = zeros(2,N); %initializing y as a zero vector of length L
y(:,1) = y0;
for i = 2:1:N
k1 = h * yd(t(i-1),y(:,i-1));
k2 = h * yd( t(i-1) + h/2 , y(:,i-1) + k1/2 );
k3 = h * yd( t(i-1) + h/2 , y(:,i-1) + k2/2 );
k4 = h * yd( t(i) , y(:,i-1) + k3 )
y(:,i) = y(:,i-1) + ( k1 + k2 + k3 + k4 )/6;
end
end
_______________________________________________________
heun function:
function [t,y] = heun(yd,tspan,y0,h)
%HEUN solves differential equations using hein's method
%yd = column vector containing the differential equations
%tspan = range of t value
%y0 = initial value
%h = interval of increment of t
t = tspan(1):h:tspan(2); %t is vector containing all the t values in increment of h.
N = length(t); %number of entries of t
y = zeros(2,N); %initializing y as a zero vector of length L
y(:,1) = y0;
% y_i+1 = y_i + phi*h
% y_i+10 = y_i + f(t_i,y_i)*h
% phi = (f(t_i,y_i) + f(t_i+1,y_i+10)) / 2)
for i = 2:1:N
y_guess = y(:,i-1) + ( yd(t(i-1),y(:,i-1) )*h);
phi = ( yd(t(i-1),y(:,i-1) ) + yd( t(i),y_guess) )/2;
y(:,i) = y(:,i-1) + phi*h;
end
end
___________________________________________________

Best Answer

yd = @(t,y) [y(2); cos(3*t) - sin(y(1));
You're missing a closing square bracket on this line.
Related Question