How to write a function in m. file?

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
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
Ragini Ravichandren el 4 de Abr. de 2021
Here's the function I passed in:
f3=@(t,y) exp(t)-t;
But after entering eulerMethod(f3, 1, 10, 0, 1), in the command window, I don't get any output.

Iniciar sesión para comentar.

 Respuesta aceptada

f3=@(t,y) exp(t)-t;
eulerMethod(f3, 1, 10, 0, 1)
Unrecognized function or variable 'tn'.

Error in solution>eulerMethod (line 15)
t=tn;
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
and indeed, you assign t=tn but tn does not exist.
Are you intended to return scalar t, and y corresponding only to the final time? Or are you intended to return a vector of times and each yn for each time ?

5 comentarios

Ragini Ravichandren
Ragini Ravichandren el 4 de Abr. de 2021
Editada: Ragini Ravichandren el 4 de Abr. de 2021
My mistake, I meant to write t=tp. And I was hoping to return vectors t and y of t and yn values for each. However, it seems like this function does neither, as it doesn't yield anything.
f3=@(t,y) exp(t)-t;
eulerMethod(f3, 1, 10, 0, 1)
ans = 10
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=tp;
y=yp;
end
That ans = 10 is the output on the command line. When you call a function with multiple outputs and you do not assign the outputs to variables, then only the first output is displayed and the rest are discarded without displaying them.
So ans = 10 is telling you that the t output of your function is 10 .
And I was hoping to return vectors t and y of t and yn values for each.
There is only one case in which MATLAB automatically saves each output of something that might vaguely be considered a loop: namely when you use spmd then the final value of each variable for each "lab" (worker) is stored in a "composite".
arrayfun and cellfun and structfun also automatically save values, but those are function-call syntax, not loops with multiple lines.
In all other cases: if you want multiple values recorded, you have to store them yourself. For example,
yp(n+1,:) = yn;
Ragini Ravichandren
Ragini Ravichandren el 5 de Abr. de 2021
Editada: Walter Roberson el 5 de Abr. de 2021
Ah OK, so I need to create a new value of yp for each iteration of the loop?
function [t,y]=eulerMethod(f3, dt, Tf, t0, y0)
n=(Tf-t0)/dt;
nf=round(n,1);
y= zeros(1, nf);
yp = zeros(1, nf);
yn = zeros(1, nf);
tp= zeros(1, nf);
yp=y0;
tp(n+1,:);
for n=0:nf
tp(n)=t0+n*dt;
f3p=f3(tp,yp);
yn(n)=yp+dt*f3p;
yp(n+1,:) = yn(n+1,:);
end
t=tp;
y=yp;
end
f3p=f3(tp,yp);
You are passing all of yp in, instead of the "current" yp
yn(n)=yp+dt*f3p;
You are calculating using all of yp, instead of the "current" yp
yn(n)=yp+dt*f3p;
The left hand side is scalar, so this code assumes that the result of f3p is scalar. That might be true for the particular function you are testing with, but your code needs to be able to handle the case where you are dealing with multiple values (for example a second order system dealing in f'' and f' )
yp(n+1,:) = yn(n+1,:);
Here you assumed yn(n+1,:) is a vector even though a second ago you assumed that yn(n) is a scalar.
yn(n+1,:) also does not exist at this point -- or rather it does, but only because you initialized to 0.
for n=0:nf
You will find the code a lot easier to write if you start n from 1 and use
tp(n)=t0+(n-1)*dt;
Or better yet, initialize tp(1) to t0, and at each step add dt to the previous tp value, instead of calculating it using n.
... You will probably find it easier to start n from 2 instead of from 1 or 0, really.
Ragini Ravichandren
Ragini Ravichandren el 5 de Abr. de 2021
Editada: Ragini Ravichandren el 5 de Abr. de 2021
Ah OK. So how would I go about preallocating the space for each value, and adding each new value to a vector for each iteration? Also, is it possible to only have the array for yp and yn, while ensuring that for each iteration, the most recent value is used? Also, I figure since I already have a loop, assigning t0+n*dt to array value tn(n) would give me the same result.

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Versión

R2020b

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by