Euler's Equation for Dummies

3 visualizaciones (últimos 30 días)
Roddy
Roddy el 1 de Oct. de 2013
Comentada: Roddy el 1 de Oct. de 2013
I have never used Matlab before and I was given these 2 scripts:
function yprime = f_outline(t,y)
%This function evaluates yprime for use in Euler's method. Use the
% subfunctions p and t to calculate yprime.
% Note: This code will not work! You have to edit it for the purposes of
% the lab
yprime = ;
function payment = p(t,y)
payment =
function rate = r(t)
rate = ;
and
%Euler's Method
% Note: This code will not work! It has holes that must be filled in.
clear all;
n = %number of steps
y = zeros(1,n); % Create a vector for the y values which will be calculated
y(1) = %initial value of y;
t(1) = 0;
%This loop computes one step of Euler's method each iteration
% Use f_outline.m to help compute each value of y.
for i = 2:n
t(i) = i*%step size;
y(i) = ;
end
plot(t,y)
My payment is 8000, my rate is 0.05, yprime=r*y*exp(r*t/(12*p))-12*p, y(1)=300000 t(1)=1 and I have to use the f_outline for y(i) with a while function until y(i)=0. My final codes look like this:
function yprime = f_outline(t,y)
yprime = r*y*exp(r*t/(12*p))-12*p;
function payment = p(t,y)
payment = 8000;
function rate = r(t)
rate = 0.05 ;
and
%Euler's Method
clear all;
n = .01
y = zeros(1,n); % Create a vector for the y values which will be calculated
y(1) = 300000
t(1) = 0;
%This loop computes one step of Euler's method each iteration
% Use f_outline.m to help compute each value of y.
while y(i)>0
for i = 2:n
t(i) = i*n
y(i) = f_outline(t(i),y(i))
end
end
plot(t,y)
but I keep getting this error:
lab1_euler_outline
n =
0.0100
Warning: Size input contains non-integer values. This will error in a future release. Use FLOOR to convert to integer values.
> In lab1_euler_outline at 4
y =
300000
Subscript indices must either be real positive integers or logicals.
Error in lab1_euler_outline (line 11)
while y(i)>0
Any help would be GREATLY appreciated.
Roddy

Respuesta aceptada

A Jenkins
A Jenkins el 1 de Oct. de 2013
You have the statement
while y(i)>0
before the loop where i is defined.
Perhaps add
i=1
before that?
  1 comentario
Roddy
Roddy el 1 de Oct. de 2013
This did not work but it got me to think that adding the
while y(i)>0
statement into the for statement which did work. I don't get an error now but I still don't get a graph. But that is another problem for another day I guess. Haha. Thanks A Jenkins!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Numerical Integration and Differential Equations en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by