Script: Indexing in function problem

조회 수: 4 (최근 30일)
Norbert Raducki
Norbert Raducki 2020년 11월 22일
댓글: Cris LaPierre 2020년 11월 26일
Hi,
I want to creat a function like this:
c(t+1,i)= c(t,i)-1/(dx^2)*(-D*(c(t,i+1)-c(t,i))-(-D*(c(t,i)-c(t,i-1)))) to solve this by ode4
and I do it like this:
but it dosen't work. I don't now why. Could someone explain to my why it dosen't work and how to indexing in fuction, pleace?

답변 (1개)

Cris LaPierre
Cris LaPierre 2020년 11월 22일
편집: Cris LaPierre 2020년 11월 24일
Functions cannot be defined in the middle of your code. You will need to either place it at the bottom of your script, or in a separate file.
See this page for more.
  댓글 수: 2
Cris LaPierre
Cris LaPierre 2020년 11월 25일
편집: Cris LaPierre 2020년 11월 25일
I could try to guess what your new code is, but it would be better if you shared your updated code first. Include the complete error message you are getting (all the red text), as well as what you are using for inputs to the function.
Cris LaPierre
Cris LaPierre 2020년 11월 26일
The error is because none of your if statements evalute to be true, so dcdt is neer assigned a value. This is because the conditional expression in an If statements only work with a single result. You define i to be a vector of numbers in your function, so it is returning a vector of results to the conditional expression.
if i is intended to be proportional to time, best to use t instead. If it is to try to collect your resutls, know that, with ode45, you do not have to worry about collecting the results of each iteration. This is handled automatically for you by the ode solver.
While not exactly the same, you may find this example helpful in setting up your problem.

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

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by