d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With Matlab > Its Supposed To Be Easy
Add Reply New Topic New Poll
Member
Posts: 6,175
Joined: Jun 22 2005
Gold: 12,542.29
May 3 2014 06:41pm
im taking number analysis (an applied math class) and my professor expects us to "figure it out" when it comes to the programming part of the class.
my goal is simple, im trying to run an ordinary differential equation through MatLab using the Runge-Kutta order 4 method (RKM04) as part of my project (the code part).
the formula and parameters for our "infection problem" are as follows:
y'(t)=kx(t)y(t)
where k is a constant and x(t)+y(t)=m, the total population
x(t): the number of susceptible individuals at time t.
y(t): the number of infected individuals at time t.
the initial value is y(0)=1000 for m=100,000, k=.000002
we want to find the number of infected individuals at t=30 days.

this is what i have so far in MatLab:

>> h = 7.5;
>> t = 0;
>> w = 1000;
>> fprintf('Step 0: t = %12.8f, w = %12.8f\n', t, w);
Step 0: t = 0.00000000, w = 1000.00000000
>> for i=1:4
k1 = h*f(t,w);
k2 = h*f(t+h/2, w+k1/2);
k3 = h*f(t+h/2, w+k2/2);

k4 = h*f(t+h, w+k3);
w = w + (k1+2*k2+2*k3+k4)/6;
t = t + h;
fprintf('Step %d: t = %6.4f, w = %18.15f\n', i, t, w);
end
Undefined function 'f' for input arguments of type 'double'.

i got most of this code from similar problems from various websites (im not good at programming at all) and i realize i need to plug my function f in somewhere but i dont know how to type it in correctly in MatLab language
i tried
>> f = @(x,y)(1/500000)*x*y
and it didnt show up as an error but now i dont know where/when its supposed to go into my code (i also dont even know if this is the correct way to plug it in).
if anyone could help me out with some EXPLICIT instructions, it would be much appreciated and i wouldnt mind sending 500fg your way if you u work through it with me (or if what u typed is enough for me to plug it all in from there)

TLDR;
-need to know how to solve an ODE using Runge-Kutta order 4 in MatLab
-need to know how to plug in my function correctly into MatLab
-sending 500fg for the person of most help so that i can get my code completed (it's actually very short, im just confused as balls)

This post was edited by hellomoto13 on May 3 2014 06:45pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 3 2014 08:00pm
f = @(x,y)(1/500000)*x*y

if i understand it correctly, your function f is:

f(x,y) = (1/500000)*x*y

?

if that is the case, you define it as so:

function result = f(x, y)
result = (1/500000)*x*y;

/edit: here's an example in their documentation: http://www.mathworks.com/help/matlab/matlab_prog/create-functions-in-files.html

or one of many youtube vids:



This post was edited by carteblanche on May 3 2014 08:05pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll