how to use for loop along with ode45 to generate a series of plots

1 回表示 (過去 30 日間)
Chaitanya Pocha
Chaitanya Pocha 2020 年 7 月 8 日
コメント済み: Chaitanya Pocha 2020 年 7 月 8 日
I'm relatively new to MATLAB. I'm trying to use ode45 to solve a second order differential and then generate a series of plots. Below is what I have done.
So, I'm given a second order differential along with boundary conditions. T(0) = 0.3 and T(1) = 1, and N that varies from 1 to 10 with an increment of 1.
This is the first function file.
function dT = func(x, T)
N = 2;
dT = [T(2); (-(4*N)-((x.^3)*T(2)))/(x.^4)];
end
This is the residual function used to solve dT/dx
function re = res(za)
[x ,T] = ode45(@func, [0.3 1], [0; za]);
re = T(end,1)-1;
end
This is the solver that I have used to generate a plot at N =2
za = fzero(@res, 50)
[x, T] = ode45(@func, [0.3 1], [0; za]);
plot(x, T(:, 1))
xlabel('Position')
ylabel('Temperature')
title('Temperature distribution in the fluid')
Okay, so the above solver gives me a plot at N =2. However, I need to get a series of 10 plots for 10 different N using a for loop. N ranges from 1 to 10 with an increment of 1. Can someone help me get 10 graphs like these?

採用された回答

Walter Roberson
Walter Roberson 2020 年 7 月 8 日
編集済み: Walter Roberson 2020 年 7 月 8 日
for N = 1 : 10
za = fzero(@(za) res(za,N), 50)
[x, T] = ode45(@(x,T)func(x,T,N), [0.3 1], [0; za]);
plot(x, T(:, 1), 'DisplayName', sprintf('N = %d', N));
if N == 1
xlabel('Position')
ylabel('Temperature')
title('Temperature distribution in the fluid')
hold on
end
legend show
drawnow();
end
hold off
function dT = func(x, T, N)
dT = [T(2); (-(4*N)-((x.^3)*T(2)))/(x.^4)];
end
function re = res(za, N)
[x ,T] = ode45(@(x,T) func(x,T,N), [0.3 1], [0; za]);
re = T(end,1)-1;
end
  8 件のコメント
Walter Roberson
Walter Roberson 2020 年 7 月 8 日
The code I posted replaces all of your previous code.
I did have a typing mistake in one line so copy it again. Put it in a file such as rez10.m and save, and run the file.
The line you are having the problem on, fzero(@res, 50) does not appear in my code.
Chaitanya Pocha
Chaitanya Pocha 2020 年 7 月 8 日
Yeah, I finally got the plot. Thanks sir walter for this. I've accepted your answer.

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

その他の回答 (1 件)

madhan ravi
madhan ravi 2020 年 7 月 8 日

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by