Clement Tham
unread,May 7, 2012, 11:13:07 PM5/7/12You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
So here's my problem.. this is the code i type to use euler's 2nd order to numerically integrate the following equation,
However, when i call the function with EulerMSM([0 8],0.2) in the command window,
this error returns,
Error in ==> EulerMSM>f_euler at 46
f_euler= -0.4*u - x - 0.5*x^3 +0.5*cos(0.5*t);
??? Output argument "maineqn" (and
maybe others) not assigned during
call to "c:\Program
Files\MATLAB\R2009a\Euler\EulerMSM.m>f_euler".
Error in ==> EulerMSM at 35
a(i) = f_euler(t(i),x(i),u(i));
The full program is as below and i've tried many things to solve this.. but still could not get it.. Any help is highly appreciated
function EulerMSM(tspan,h)
% EulerMSM(dydt,tspan,y0,h):
% uses Euler's method to integrate an ODE
% input:
% dydt = name of the M-file that evaluates the ODE
% tspan = [ti, tf] where ti and tf = initial and
% final values of independent variable
% y0 = initial value of dependent variable
% h = step size
% output:
% t = vector of independent variable
% y = vector of solution for dependent variable
ti = tspan(1)+1;
tf = tspan(2)+1;
time = (ti:h:tf);
N = length(time);
% if necessary, add an additional value of t
% so that range goes from t = ti to tf
%declaring variables
t = zeros(N+1,1);
x = zeros(N+1,1);
u = zeros(N+1,1);
a = zeros(N+1,1);
b = zeros(N+1,1);
c = zeros(N+1,1);
%initial Conditions
t(1) = 0;
x(1) = 0;
u(1) = 0;
%implement Euler's 2nd order method in for loop
for i = ti:h:tf
a(i) = f_euler(t(i),x(i),u(i));
t(i+1) = t_i(t(i),h);
x(i+1) = x_i(x(i),h,u(i));
u(i+1) = u_i(u(i),h,f_euler(i));
end
theoutput = [t x u a]
%the main function to be solved
function maineqn = f_euler(t,x,u)
f_euler= -0.4*u - x - 0.5*x^3 +0.5*cos(0.5*t);
%discretized equation below
function t = t_i(t,h)
t_i = t + h;
function x = x_i(x,h,u)
x_i = x + h*u;
function u = u_i(u,h,f)
u_i = u + h * f;