How to write a function in m. file?

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Walter Roberson
Walter Roberson il 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
Ragini Ravichandren il 4 Apr 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.

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 4 Apr 2021
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 Commenti
Walter Roberson
Walter Roberson il 5 Apr 2021
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 il 5 Apr 2021
Modificato: Ragini Ravichandren il 5 Apr 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.

Accedi per commentare.

Più risposte (0)

Tag

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by