How to write a function in m. file?
Show older comments
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 Comments
Walter Roberson
on 4 Apr 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
on 4 Apr 2021
Accepted Answer
More Answers (0)
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!