필터 지우기
필터 지우기

Initial Value of y(0)=0

조회 수: 6 (최근 30일)
Karen
Karen 2011년 11월 23일
How does one write a code for the IVP of y(0) = 0? The program won't accept the following. Also, can someone check my syntax for the equations I've entered? Thanks!
function ystar = Eulermethod201(n)
a=0;
b=5;
h=(b-a)/n;
t=0:h:5;%Initialize time variable
clear ystar;%wipe out old variable
ystar(0)=0;%Initial condition (same for approximation)
for i=0:length(t), %Set up "for" loop
%Calculate the derivative
k1=(-0.5*exp(t(i)/2)*sin(5*t(i))+5*exp(t(i)/2)*cos(5*t(i))-ystar(i));
ystar(i+1)=ystar(i)+h*k1;%Estimate new value of y
end
%Exact solution
y=(exp(t/2))*(sin(5*t));
%Error calculation
percent_error=100*abs(y-ystar)./y;
disp(percent_error);
%Plot approximate and exact solutions
plot(t,ystar,'b--',t,y,'r-',t,percent_error,'g');
legend('Approximate','Exact','Error');
title('Euler Approximation n=50');
xlabel('Time');
ylabel('y*(t), y(t)');

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 11월 23일
Unlike C language, MATLAB uses 1-based index for vector and matrix. So if you define a 10x1 vector, a=rand(10,1), you refer it as a(1), a(2) till a(10).
If you have the need to indicate some value at time==0, you will need to use some kind of offset to deal with it.
  댓글 수: 2
Karen
Karen 2011년 11월 24일
If y(0)=0 is an initial value, then what type of offset would work in that situation? Does that mean that I can use y(1) = 0? Or subtract one as in i=1:length(t)-1; and also use y(1)=0? I've tried using y(.000001)=0, but I get the same error message as if I used y(0)=0.
Fangjun Jiang
Fangjun Jiang 2011년 11월 24일
Yes. You can't use y(0)=0 in MATLAB. You will need to do something like this for example
t=0:9;
y=10:10:100;
For any index number i, y(i) always corresponds to t(i), where i is a number from 1 to 10.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by