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}

2. 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}
So what have you done so far?

CB

3. Code:
function [t,U]=minPrim(f,[I0,I],U0,N)

NrIn=nargin;
if NrIn==?

elseif NrIn=?
N=100;
else
error(’Wrong number of input arguments!’)
end
U=zeros(1,N+1);

k=(I-I0)/N %increment
t=I0:k:I %Vector with all t:s
U(1)=U0;
for k=2:?
U(?)=U(?)+k*f(?);
end

This is what i have done so far. I dont know how to apply this in the loop.
Because U dependes botn on the i:th intervall and the position t.

A guess from my side is:

for 2:k:N
U(k)=U(k-1)+k*f(???)
end

4. Originally Posted by techmath
Code:
function [t,U]=minPrim(f,[I0,I],U0,N)

NrIn=nargin;
if NrIn==?

elseif NrIn=?
N=100;
else
error(’Wrong number of input arguments!’)
end
U=zeros(1,N+1);

k=(I-I0)/N %increment
t=I0:k:I %Vector with all t:s
U(1)=U0;
for k=2:?
U(?)=U(?)+k*f(?);
end

This is what i have done so far. I dont know how to apply this in the loop.
Because U dependes botn on the i:th intervall and the position t.

A guess from my side is:

for 2:k:N
U(k)=U(k-1)+k*f(???)
end

]
Code:
function [t,U]=minPrim(f,[I0,I],U0,N)

NrIn=nargin;
if NrIn==?

elseif NrIn=?
N=100;
else
error(’Wrong number of input arguments!’)
end
U=zeros(1,N+1);

k=(I-I0)/N %increment
t=I0:k:I %Vector with all t:s
U(1)=U0;
for ik=2:length(U)
U(ik)=U(ik-1)+k*f(t(ik-1),U(ik-1));
end
Assuming you have defined the derivative as U'(t)=f(t,U)

CB

5. Yes that is correct.

I dont know for example how it is possible to put a function f as argument. I thought it was only possible to but in values directly, for example:

6. Originally Posted by techmath
Yes that is correct.

I dont know for example how it is possible to put a function f as argument. I thought it was only possible to but in values directly, for example:

Research Matlab function handle Handle used in calling functions indirectly - MATLAB

CB

7. OK, i think i have to use it like this then:

minPrim(@functionname,[I0,I],U0,N)

So i make a new file with the function i want to use.

But i have an error, it is at the first row. The vector [I0,I], maybe i didnt express it correctly...

??? Error: File: minPrim.m Line: 1 Column: 28
Unbalanced or unexpected parenthesis or bracket.

8. Originally Posted by techmath
OK, i think i have to use it like this then:

minPrim(@functionname,[I0,I],U0,N)

So i make a new file with the function i want to use.

But i have an error, it is at the first row. The vector [I0,I], maybe i didnt express it correctly...

??? Error: File: minPrim.m Line: 1 Column: 28
Unbalanced or unexpected parenthesis or bracket.
remove the square brackets around [I0,I] since you are treating I0 and I as variables in your code

CB

9. Okay, i assume i have to change the if thesis to 5 input arguments.

Code:
function [t,U]=minPrim(f,I0,I,U0,N)

NrIn=nargin;
if NrIn==5

elseif NrIn==5
The new error message is:

??? Error using ==> functionname
Too many input arguments.

Error in ==> minPrim at 17
U(ik)=U(ik-1)+k*f(t(ik-1),U(ik-1));

Code:
function [y]=functionname(x)

y=x^2;

It is strange that the expression inside the foor loop is incorrect when i try to run the program. Because in the file there is no indication that something is wrong (by showing red marks)

10. Originally Posted by techmath
Okay, i assume i have to change the if thesis to 5 input arguments.

Code:
function [t,U]=minPrim(f,I0,I,U0,N)

NrIn=nargin;
if NrIn==5

elseif NrIn==5
The new error message is:

??? Error using ==> functionname
Too many input arguments.

Error in ==> minPrim at 17
U(ik)=U(ik-1)+k*f(t(ik-1),U(ik-1));

Code:
function [y]=functionname(x)

y=x^2;

It is strange that the expression inside the foor loop is incorrect when i try to run the program. Because in the file there is no indication that something is wrong (by showing red marks)
You do not need the time variable in the function call as the function is defined without it.

CB

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;