how to use for loop along with ode45 to generate a series of plots
1 view (last 30 days)
Show older comments
Chaitanya Pocha
on 8 Jul 2020
Commented: Chaitanya Pocha
on 8 Jul 2020
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?
0 Comments
Accepted Answer
Walter Roberson
on 8 Jul 2020
Edited: Walter Roberson
on 8 Jul 2020
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 Comments
Walter Roberson
on 8 Jul 2020
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.
More Answers (1)
madhan ravi
on 8 Jul 2020
https://www.mathworks.com/help/matlab/math/parameterizing-functions.html after that loop through N.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!