フィルターのクリア

Euler's method

1 回表示 (過去 30 日間)
Ahmet Akcura
Ahmet Akcura 2021 年 8 月 22 日
コメント済み: Wan Ji 2021 年 8 月 23 日
Hello,
Where is the problem? Can you look at it?
h=0.25;
a=1; %start
b=2; %end
n=5; %iteration number
x=zeros(n,1);
y=zeros(n,1);
x=linspace(a,b,n);
y(1)=2;
for i= 1:n-1
y(i+1)=y(i)+ h*(((sin(2*x))/x^2)-(2*y/x));
end
[x y]
Unable to perform assignment because the left and right sides have a different number of
elements.
Error in foo (line 16)
y(i+1)=y(i)+ h.*(((sin(2.*x))./x.^2)-(2.*y./x));

回答 (1 件)

Wan Ji
Wan Ji 2021 年 8 月 22 日
Hi,
Use
y(i+1)=y(i)+ h*(((sin(2*x(i)))/x(i)^2)-(2*y(i)/x(i)));
instead of
y(i+1)=y(i)+ h*(((sin(2*x))/x^2)-(2*y/x));
  2 件のコメント
Ahmet Akcura
Ahmet Akcura 2021 年 8 月 22 日
Again there is an errror;
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in foo (line 18)
[x y]
Wan Ji
Wan Ji 2021 年 8 月 23 日
Your x array is not transposed, its size 1 by n while y array is n by 1. So I transpose it for you.
h=0.25;
a=1; %start
b=2; %end
n=5; %iteration number
y=zeros(n,1);
x=linspace(a,b,n)';
y(1)=2;
for i= 1:n-1
y(i+1)=y(i)+ h*(((sin(2*x(i)))/x(i)^2)-(2*y(i)/x(i)));
end
[x y]
plot(x,y,'r-o') % plot the result
xlabel('x'); ylabel('y')

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDigital Filter Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by