Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

??? Output argument "r" (and maybe others) not assigned during call to

721 views
Skip to first unread message

Clement Tham

unread,
May 7, 2012, 11:13:07 PM5/7/12
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;

Nasser M. Abbasi

unread,
May 7, 2012, 11:18:20 PM5/7/12
to
On 5/7/2012 10:13 PM, Clement Tham wrote:

>
>
> 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

did you try using the DEBUGGER and set break point on error?

the debugger will stop at the line where the error is.

Then all what you have to do, is just look at the line, and
examine the variables and read the error message to see
what the problem is. This is called debugging.

hth

--Nasser

Clement Tham

unread,
May 7, 2012, 11:20:08 PM5/7/12
to
and on highlighting the maineqn,
this message came up

MCC requires the program to assign a value to output argument

Clement Tham

unread,
May 7, 2012, 11:24:08 PM5/7/12
to
"Nasser M. Abbasi" <n...@12000.org> wrote in message <joa39s$3a5$1...@speranza.aioe.org>...
have tried that.

my initial conditions for t=u=x=0;

and the f_euler shows the answer as 0.500 which is correct
then it exits with the error.

what could be the problem?

Thank you

Nasser M. Abbasi

unread,
May 7, 2012, 11:35:43 PM5/7/12
to
>
> what could be the problem?
>
> Thank you

Looking at the code:

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

you did NOT return back t and y

dont you need it to be like this:

function [t,y] = EulerMSM(tspan,h)

?

--Nasser

Clement Tham

unread,
May 7, 2012, 11:55:05 PM5/7/12
to
"Nasser M. Abbasi" <n...@12000.org> wrote in message <joa4af$558$1...@speranza.aioe.org>...
Sorry, am new to matlab. didnt know that it should be function [t,y]

i found out something, when calling the function
t(i+1) = t_i(t(i),h);

where t_i = t + h;
after the action, the t(i) is not updated with the new value which should be t(1)=0 t(2)=0.2

what was my mistake?

Clement Tham

unread,
May 8, 2012, 12:35:07 AM5/8/12
to
Argh! i think i know my mistake

function x = x_i(x,h,u)
x_i = x + h*u;
end

should be

function x = x_i(x,h,u)
x = x + h*u;
end

whole night cant sleep because of this silly mistake

Nasser M. Abbasi

unread,
May 8, 2012, 1:01:01 AM5/8/12
to
Well, when I face a strange problem that I do not
understand because it sits inside complicated
code, I try to reproduce the problem on the side, using
the most simple code structure possible by removing all
needed layers and peripheral code.

Almost always, I find the problem that way, because things
now become clear when all the clutter was removed.

--Nasser

Clement Tham

unread,
May 8, 2012, 1:23:07 AM5/8/12
to
> Well, when I face a strange problem that I do not
> understand because it sits inside complicated
> code, I try to reproduce the problem on the side, using
> the most simple code structure possible by removing all
> needed layers and peripheral code.
>
> Almost always, I find the problem that way, because things
> now become clear when all the clutter was removed.
>
> --Nasser

Thanks for the advice. will keep it in mind.. still lots to learn from matlab..
0 new messages