Originally Posted by

**techmath** Need to finish a program that approximates a first order differential equation with eulers method.

Code:

function [t,U]=minPrim(?,?,?,?)
NrIn=nargin;
if NrIn==?
elseif NrIn=?
N=100;
else
error(’Wrong number of input arguments!’)
end
U=zeros(1,N+1);
k=? %increment
t=? %Vector with all t:s
U(1)=U0;
for k=2:?
U(?)=U(?)+k*f(?);
end

4 input arguments

f, function

Vector with rand values (2 of them, this is the intervall)

Initial value U0

Number if intervalls N.

This should solve for example:

u'=x^2

u0=3

{x:0,2}

There is a very good book written by Timothy Sauer called "Numerical Analysis" that you should take a look at. Here is the companion website:

Numerical Analysis

This might not be exactly what you are after, but it will give you a good platform. If you can get this book from your library I would recommend it as it will help you understand the method. Here is some code within the book (available from the link above):

Code:

%Program 6.1 Euler's Method for Solving Initial Value Problems
%Use with ydot.m to evaluate rhs of differential equation
% Input: interval [a,b], initial value y0, step size h
% Output: time steps t, solution y
% Example usage: y=euler([0 1],1,0.1);
function [t,y]=euler(int,y0,h)
t(1)=int(1); y(1)=y0;
n=round((int(2)-int(1))/h);
for i=1:n
t(i+1)=t(i)+h;
y(i+1)=eulerstep(t(i),y(i),h);
end
plot(t,y)
function y=eulerstep(t,y,h)
%one step of Euler's Method
%Input: current time t, current value y, stepsize h
%Output: approximate solution value at time t+h
y=y+h*ydot(t,y);
function z=ydot(t,y)
z = t*y + t^3;