How to plot an array graph from a 'for' loop?

7 ビュー (過去 30 日間)
Savely Chernyshev
Savely Chernyshev 2022 年 9 月 7 日
コメント済み: Savely Chernyshev 2022 年 9 月 7 日
I'm trying to plot an analytical solution to the equation and an approximate one obtained by the Euler scheme . Here 's what happened:
Question: how to plot an iterative solution x(i) not by index i, but by variable x = 0:0.1:10? To make it possible to compare the graphs adequately
Thank you!
x(1) = 1;
delta_t = 0.1;
n = 100;
for i = 1:n
hold on
grid on
x(i+1) = x(i)*(1-delta_t);
plot(i,x(i),'r.-')
ezplot('exp(-x)',[0 10]);
end

採用された回答

Alan Stevens
Alan Stevens 2022 年 9 月 7 日
Try multiplying i by delta_t in the plot command
x(1) = 1;
delta_t = 0.1;
n = 100;
for i = 1:n
hold on
grid on
x(i+1) = x(i)*(1-delta_t);
plot(i*delta_t,x(i),'r.-')
ezplot('exp(-x)',[0 10]);
end
  1 件のコメント
Savely Chernyshev
Savely Chernyshev 2022 年 9 月 7 日
Thank you very much!

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

その他の回答 (0 件)

カテゴリ

Find more on Line Plots in Help Center and File Exchange

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by