I know why the error is the zeroes?

1 回表示 (過去 30 日間)
Ous Chkiri
Ous Chkiri 2019 年 11 月 7 日
編集済み: Hiroki Okawa 2019 年 11 月 8 日
clear all;
close all;
clc;
dt=0.25;
x=-40:450;
ui=0.5.*exp(-log(2).*(x./3).^2);
figure(1)
plot (x,ui)
xlabel('x-axis')
ylabel('u(x,0)')
xlim([-40 450])
ui=0.5.*exp(-log(2).*((x-400)./3).^2);
figure(2)
plot (x,ui)
xlabel('x-axis')
ylabel('u(x,400)')
xlim([350 450])
ylim([-0.2 0.6])
%%Central difference
unew=zeroes(1,471);
uold=ui;
for j=1:1600
unew(1)=uold(1)-dt*((uold(2)-uold(1))/2);
for i=2:length(ui)-1
unew=uold(i)-dt*((uold(i+1)-uold(i-1))/2);
end
uold=unew;
j*dt;
end
figure
plot(x,ui(x,400))
hold on
plot(x,unew)
hold off
title('central difference @dt=0.25')
  2 件のコメント
Star Strider
Star Strider 2019 年 11 月 7 日
Not spelled correctly. Should be:
unew = zeros(1,471);
darova
darova 2019 年 11 月 7 日
unew has no index?
unew=uold(i)-dt*((uold(i+1)-uold(i-1))/2);

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

回答 (1 件)

Hiroki Okawa
Hiroki Okawa 2019 年 11 月 8 日
編集済み: Hiroki Okawa 2019 年 11 月 8 日
1.
% unew = zeros(1,471);
unew = zeros(size(ui));
2.
% unew=uold(i)-dt*((uold(i+1)-uold(i-1))/2);
unew(i)=uold(i)-dt*((uold(i+1)-uold(i-1))/2);
3.
% plot(x,ui(x,400))
plot(x,ui)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by