How to write a function in m. file?

조회 수: 2 (최근 30일)
Ragini Ravichandren
Ragini Ravichandren 2021년 4월 4일
편집: Ragini Ravichandren 2021년 4월 5일
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
Walter Roberson
Walter Roberson 2021년 4월 4일
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 2021년 4월 4일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 4월 4일
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
Walter Roberson
Walter Roberson 2021년 4월 5일
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 2021년 4월 5일
편집: Ragini Ravichandren 2021년 4월 5일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by