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

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Programming 的更多信息

标签

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by