How to write a function in m. file?
Mostrar comentarios más antiguos
I've been trying to write the following function in an m.file, that will yield values t, and y as outputs:
function [t,y]=eulerMethod(f3, dt, Tf, t0, y0)
syms n;
n=(Tf-t0)/dt;
nf=round(n,1);
yp=y0;
for n=0:nf
tp=t0+n*dt;
f3p=f3(tp,yp);
yn=yp+dt*f3p;
yp=yn;
end
t=tn;
y=yp;
end
However, upon running the fuction
[t,y]=eulerMethod(f3, 1, 10, 0, 1)
I get nothing. Do I need to declare t and y at the beginning, or return them at the end?
2 comentarios
Walter Roberson
el 4 de Abr. de 2021
You do not need to declare those or return them.
I do not see anything obviously wrong with the code. What is your f3 that you are passing in?
It is confusing that you use n for three different purposes. The syms n is completely unnecessary in this context.
Ragini Ravichandren
el 4 de Abr. de 2021
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Common Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!