MATLAB Help - Finite Difference Method
HTML-код
- Опубликовано: 23 авг 2024
- If you'd like to use RK4 in conjunction with the Finite Difference Method watch this video • MATLAB Help - Finite D...
Code in this video
github.com/cmo...
Textbook on Numerical Methods
github.com/cmo...
I cracked up at the "what does this mean" part! Awesome and helpful video!
for such a serious video that moment of silliness was just perfect
Thanks Dr. It is really helpful and I learnt FDM by watching only this video. Appreciate it.
'△t
I'm glad that you're an aerospace engineer - me too :'D
This helped my tutoring career
Holy shit you came in so clutch with this thanks
ruclips.net/video/R8F7KfY1Lvo/видео.html
Great vid, looking forward to more. Your vids are awesome.
Hey! That helped a lot. I am currently struggling with a question that has been set and I was hoping that you could lend a hand?
A subterranean structure is to be constructed in the vicinity of a tidal river. For
simplicity we will assume that the site is at a constant level. Using a finite difference analysis
you must analyse the underlying groundwater level and propose an appropriate pumping
network to aid construction. The river through the site can be described parametrically
with t ∈ [0, 1].
x = 2000t
y = 462 ± 15 − 25.4t − 4028t^2 + 7004t^3 − 2763t^4
The total extent of the site is a rectangle of Lx = 2000 m Ly = 1000 m, with
K = 6.2 × 10^−4 ms−1. Note that where river intersects the site the groundwater level
may be assumed to be 0 m. The boundaries of the site may be modelled such that the
y = 0 m has a head of 3 m and y = 1000 m has a head of 5 m. As the variation along the
x boundaries are not known we assume that the flow is perpendicular to the boundary. i.e.
there is no variation in head in the y direction.
B1. The groundwater level across the site can be approximated using the Laplace equation.
Following a similar iterative approach to the tutorial examples, solve Laplace’s equation
and obtain the steady state water level within the site. Once you have obtained the
hydraulic head determine the flux field to indicate the flow of water.
B2. An excavation in the site, of size a = 100 m and b = 75 m, located at Px, Py=(950, 500)
m is required. To ease the process of construction the water level must be lowered in
the region where construction will proceed. The water level must be lowered to below
-2 m within the site using a series of pumps, with a pumping rate of −1.3 × 10−3s^−1.
You may select the position and number of pumps, they do not have to be within the
site, but they should not be co-located - you can’t have more than one pump within 25
m of another. There is no restriction on the number of pumps but you should attempt
to find an efficient design.
You should design your code so it is possible to trial various pumping schemes and it
should be clear how you have tested the success of your scheme. Compare the flux
field with that obtained for a site with no pumping. Indicate clearly the location of
your pumps.
+Ph Tang Unfortunately I have never seen a problem like this. This seems like a civil engineering question and I am an Aerospace Engineer. Unfortunately you will have to ask someone else.
Really great videos Sir....fantastic work
Carlos, what a good video and explanation!
I have a question: Why for the discretization of time (Stability criterion - Carnahan), do you multiply by 0.5 and then divide in two again? It should not be (dx ^ 2)/(2 * k), without the 0.5. I am right?
I think I was just messing around with numbers. I believe you have it right. It should just be over 2.
Thank you, Carlos!
Hi Carlos... would it be possible for you to make a tutorial on plate bending with different support conditions using finite difference method?
Unfortunately this is not my immediate area of expertise. I'm an aerospace engineer so I can't help you with this.
Thank you so much, this was very helpful to me!! Can I use this very method to solve problems with some others boundary conditions?
Yes of course. Just change lines 34 and 35. T_mat(1,:) = 200 etc.
I mean, others like Neumann boundary conditions
If you want to use Neumann boundary conditions the equation on line 7 changes since the boundary condition is now a derivative. I have those equations lying around in my notes somewhere but overall the code is very similar.
Thank you so much, that was clarifying!
Interest Video Sir, You are very smart, I have question, how to explain heat transfer on welding use goldaks double elipsoidal formula?
Unfortunately I'm not an expert in that field so I can't help you.
@@CarlosMontalvo251 thanks, it's nice to see you
Thank you so much for such content, what I would like to ask is what if my initial condition at T(x,0) is a function of x , then how can i insert such a condition to your code also one more thing is that what if i want to analyze such system with Neumann boundrey conditions. Can you please help me with that?
At 6:45 you'll notice that line 34 says T_mat(1,:) = 200; That means that there is a boundary condition where the left end of the pipe is always 200. On line 35 I have that the right end of the pipe is 150. If you want to set the initial condition of the pipe you'll need to put
T_mat(2:end-1,1) = 100;
That line of code says that for the first timestamp from the 2nd point on the pipe to the point right before the end the pipe temp is 100. I hope that helps.
Oh and for Neumann boundary conditions you need to redo the equations of motion and you'd have to redo a lot of the code. I'd recommend doing it by hand first.
@@CarlosMontalvo251 Thank you so much
Thanks man.. you have no idea how much this helped me. can you demonstrate neumann / adibatic boundary condition ?
If you want to use Neumann boundary conditions the equation on line 7 changes since the boundary condition is now a derivative. I have those equations lying around in my notes somewhere but overall the code is very similar.
Hey! How do you implement an implicit method in this? What you have done is an explicit method of solving. Implicit method involves TriDiagonal Matrix, and I'm stuck there. Would be of great help if you could do a video on that? Or just guide me, either ways it's fine.
Thank you for saving my life
ruclips.net/video/R8F7KfY1Lvo/видео.html
Great video and thank you! What is the title of the text you referred to for Carnahan stability condition?
I have my own textbook that I've written open source github.com/cmontalvo251/MATLAB/blob/master/Montalvo_Numerical_Methods_Textbook.pdf but I also use a textbook by Chapra www.amazon.com/Numerical-Methods-Engineers-Steven-Chapra/dp/0073401064
@@CarlosMontalvo251 wonderful! again, thank you!
Dr Montalvo, if you want to use U values for thermal resistance instead of K values how will this effect your governing equation ?
The governing equation is on line 7 with k values. This is the only equation I'm familiar with. If you change it to U values you'd have to go look in a thermodynamics textbook to find the answer. If you have the governing equation then you just rederive the equations of motion like I did.
Carlos, would this method be possible if Temperature is based on three coordinates, namely x,y,z? Also, the shape I'm working with is a right triangular prism.
yes 3D is entirely possible
May I ask about the link to the textbook on numerical methods mentioned in this video ?
Yes my apologies. The link was broken. Here's the link to the code and the book.
Code in this video
github.com/cmontalvo251/MATLAB/blob/master/screen_cast_codes/Heat_Equation/finite_difference_example.m
Textbook on Numerical Methods
github.com/cmontalvo251/LaTeX/blob/master/Numerical_Methods_Montalvo/Numerical_Methods.pdf
this video really helped me a lot.Please do you could do a little video that explains the variation of temperature over time?
Are you talking about the underlying physical phenomena? Because if so that seems more well suited for a different channel.
in fact it is a question of tracing the evolution of the temperature as a function of the time since in the video you did it according to x and the time. I would like to know how to do it on matlab.
if possible now since I work on it and I am blocked. An explanation on it will help me but a small video will be able to help me even more
in general i just want to know how we can plot the evolution of heat as a function of time
@@bertinekouekam8648 I don't understand your question. At 11:08 I show a plot with position of the pipe on the x-axis and time on the y-axis. You're asking how to plot temperature as a function of time but I already did that.
i want to have something in 2D with heat on y axis and time on x axis a thing like that T=f(t).i don't know if you understand me know
That was very helpful. Can I apply this idea to T(x,y,z,t)?
Although I've never done it myself I am pretty sure that you can.
Hey Dr.,
I am working on same equation with finite element method and getting negative temperature after 3rd time step can you suggest me a video to solve this same problem with FEM?
Thank you,
Paras.
I cannot unfortunately. I myself have never coded an Finite Element Method
Thanks Dr. Very nice and informative lecture.
Can you please make a lecture on how to solve (i) nonlinear system of ODE with reaction diffusion term, (ii) solving nonlinear system of coupled ODE; both using finite difference scheme
Unfortunately this video here is the limit of my knowledge. I am an Aerospace Engineer and just teach Numerical Methods at the UG level.
@@CarlosMontalvo251 Thanks for the response. I can send you some references, may be it could work out for us.
@@swatityagi4358 Sure send it over. It might spark some interest.
@@CarlosMontalvo251 can you give me your email id, so that I can share.
@@swatityagi4358 Just post them here.
which book did you refers for this problem , the book you showed in pdf , may i know the title of the book ? tq
The pdf itself is notes I've created in Latex but all of those equations came from www.amazon.com/Numerical-Methods-Engineers-Steven-Chapra/dp/0073401064
@@CarlosMontalvo251 tq dr carlos
Thank you ,,it was helpful,
How can we do this with source or sink
ruclips.net/video/R8F7KfY1Lvo/видео.html
Hi i have done some works in my college of mechanical engineering using mathlab i could say i am in a intermediary user. Could u please post the others programs u have used to get this graphic.
I no longer post code to my videos anymore. If you need the code you will just have to watch the video and follow long.
Hi Dr
Please. I needed an algorithm to solve the equations Telegraphic equations or Propagation equations of transmission line by method FDTD.( for my university project)
I don't know what telegraphic equations or propagation equations are. I am an aerospace engineer. You will have to ask someone else.
Dr. Carlos Montalvo they are bulk matter equations in electrical engineering (edit: and applied physics) that model the propagation of electromagnetic fields along a wire. The model assumes microscopic resistance, capacitance, and inductance of finite elements of the wire. The equations are called the “telegrapher equations” and resemble modified wave equations. Typically, in electrical circuit design we assume that the bulb or whatever turns on immediately after the switch is turned on. However, a tiny amount of time is required to transmit this signal. For high frequency circuits or dimensionally large circuits (relative to the wavelength of the incident radiation in the wire) we need to account for this delay, hence the discretization of the conductor via telegrapher equations. Another approach is to solve the Maxwell equations of electromagnetism, it this requires an understanding of Green’s functions and potential functions (which isn’t too bad once you look at the math). Usually, such math cannot be resolved analytically, which I guess is why the OP was watching this video. I’m a dropout (cashier) so I have limited experience & training with these things lol. I just heard about these equations in the grapevine. Awesome channel btw 100% subbed 😊👏🏽🎊🥇🤓
Dr. Carlos Montalvo just outta curiosity, what do aerospace engineers do?
That was very helpful! I would really appreciate if you can solve a nonlinear elliptic PDE with FDM method.
hey no problem. can you give me an example?
+Carlos Montalvo
This is the PDE I was trying to solve:
U′′x−U′x+U2=cos2(x)−sin(x)+cos(x)
in which, exact solution is:
U=cos(x).
I had problem with applying Neumann BC to it. Dirichlet is working fine but not von Neumann.
+Carlos Montalvo
the equation:
www.hostmath.com/Show.aspx?Code=U_x%5E%7B%5Cprime%5Cprime%7D-U_x%5E%5Cprime%2BU%5E%7B2%7D%3Dcos%5E%7B2%7D(x)-sin(x)%2Bcos(x)
I'll take a look and get back to you in a few days. Thanks for the problem!
+Carlos Montalvo Hey I got it to work. Give me a day or so to post the solution. I ended up using a blended RK4 FDM approach which has pretty good convergence. Obviously there are other ways but the method I will post does just fine.
Thanks for Your efforts. Need the name or link for the documents which You are coding. Looking forward to hear from You. I will be tankful-for this act of kindness
You're welcome. The following text has been added to the description
Code in this video
github.com/cmontalvo251/MATLAB/blob/master/Screen_Cast_Codes/Heat_Equation/finite_difference_example.m
Textbook on Numerical Methods
github.com/cmontalvo251/MATLAB/blob/master/Numerical_Methods_Montalvo/Numerical_Methods.pdf
How do I plot the temperature versus the distance(for a particular given time..say at the end of 10min I want a plot of T vs x) in your problem?
plot(x_vec,T_mat(:,end))
Let me explain. x_vec is the vector created that contains the x-coordinates. T_mat is a matrix. The number of rows is the same as the length of x_vec and the number of columns contains each temperature profile for each time. So plotting T_mat(:,end) plots the last time slot T_mat(:,2) would plot the second time slot or rather the second timestep.
Thanks a ton!
helpful video
why did you write tdx for integration step?
I typically use idx which is short for index but in this case I decided to use tdx which is short for time index.
please can i have the book that you use in this video?
Numerical Methods by Chapra
I'm trying to solve 1D laplace heat equation for a plate using the FDM. Do you have any advice as to how i can the code step by step and plot contours as my matlab skills are mediocre.
Are you looking for something like this?
purge
x = 1:3;
y = 1:3;
[xx,yy] = meshgrid(x,y);
T00 = (75+0)/2;
T44 = (75+50)/2;
T04 = (100+75)/2;
T40 = (0+50)/2;
A = [ 4 -1 0 -1 0 0 0 0 0;
-1 4 -1 0 -1 0 0 0 0;
0 -1 4 0 0 -1 0 0 0;
-1 0 0 4 -1 0 -1 0 0;
0 -1 0 -1 4 -1 0 -1 0;
0 0 -1 0 -1 4 0 0 -1;
0 0 0 -1 0 0 4 -1 0;
0 0 0 0 -1 0 -1 4 -1;
0 0 0 0 0 -1 0 -1 4];
b = [75; 0; 50; 75; 0; 50; 175; 100; 150];
xans = A\b;
Tsolution = wrapmatrix(xans)';
%Tsolution = [43 33.3 33.89;63.21 56.11 52.34;78.59 76.06 69.71];
plottool(1,'Heat 2D',12,'x (m)','y (m)','Temperacture (Celsius)',['Heat' ...
' 2D'])
%mesh(xx,yy,Tsolution)
T = [0 0 0 0 0;75 75 60 50 50;75 75 60 50 50;75 75 60 50 50;100 100 100 100 100];
for iter = 1:10
for idx = 2:4
for jdx = 2:4
T(idx,jdx) = (T(idx+1,jdx) + T(idx-1,jdx) + T(idx,jdx+1) + T(idx,jdx-1))/4;
end
end
end
plottool(1,'Heat 2D',12,'x (m)','y (m)','Temperacture (Celsius)','Numerical Error')
mesh(xx,yy,T(2:4,2:4)-Tsolution)
%%%Iterative Method
comment out plottool and purge oh and comment out wrap matrix too.
Hmm. I'm not sure that code will run easily. This seems more complex than I thought.
this could be helpful thanks
Hi! Can you please help me on how to solve with finite difference method and ode45 put together (rather than using RK4)?
I don't think that would work to be honest. ode45 uses an adaptive timestep and finite difference method is a fixed timestep. But maybe it would work? Seems kind of difficult though.
Thanks for the suggestion.If you could help me how to solve simultaneous differential algebraic equations(1st order) where one initial condition is known(It is required as we have to discretize the space not the time)...then I could probably do it myself
Simultaneous differential algebraic equations? I don't think I've ever heard of those. Can you give me an example?
Great
I Dr. Montalvo, good video and good work; do you share a file.m? thank you
For code see git@gitlab.com:Montalvo/MATLAB_Games.git - Screen_Cast_Codes - Finite_Difference_Method
Thank you very much :-)
what if our boundary conditions wasn't constant?
You'd have to use Neumann conditions I think and redirve the equations. Unforunately I don't recall how to do that
Hi I need cod finite difference method for wave equation
I do not have code for wave equation.
12:03 , what book do you use? thanks
Numerical Methods for Engineers by Chapra.
the ordinary differential method can be done in the same way,right??
Anika Esha not sure what you mean. what is the ordinary differential method? and what is this way?
Dr. Carlos Montalvo well what you have done it's partial differential equation right?so I was just asking if the ordinary differential equation can be done in the same way...
is this direct way and can apply this direct method on 2-d fisher's equation
This is the indirect method. Also see this for Fisher equation. www.sciencedirect.com/science/article/pii/S0895717797000150/pdf?md5=0165428fb9a79afade595e3eb72bd48e&pid=1-s2.0-S0895717797000150-main.pdf&_valck=1
I am solving four coupled pdes in 1D and want to make one dependent variable value constant after particular time so it cant go below zero and I will use its zero value for further simulation to get desired value of other dependent variables.
I would make that variable a global variable so that when you change it in one place it will change it everywhere
@@CarlosMontalvo251 ok sir if we want to stop one variable after some time so it can not affect the other one how we can do that in the coupled pdes system
@@bediumang if you're using ode45 that may be more difficult. However if you are using RK4, you can set the variable to a constant in your for loop. That might be an interesting video to make.
Dr. Carlos Montalvo actually I am using ode45 and ode23tb .please make one Vedio its really helpful
@@bediumang i would look at one of my Rk4 videos first to see how to do that.
Good day Dr. Carlos, May God bless you for putting off such tutorial. please I still need your help for 2D PDE TUTORIAL (I MWAEN YOU SHOULD EXTEND THE ABOVE TUTORIAL TO 2D). THANKS
Can you give me an example?
@@CarlosMontalvo251
δT/δt=-u δT/δx+1/ρCp [k_ex (δ^2 T)/(δx^2 )+k_er (1/r δT/δx+(δ^2 T)/〖δx〗^2 )+ ρ_s (1-ε) H n(r_co )]
The initial conditions for the continuity and energy models are
at t=0,T=T_0
The boundary conditions are:
.At Z=0,,〖-k〗_ez δT/δz=ρuC_p (T_0-T)
At z=L, δT/δz=0
At r=0, δT/δr=0
At r=R, -k_ez δT/δr=h_w (T-T_w)
u= 0.5; %m/s
gs=7633.65; %kg/m3
ef=0.7;
e=0.3;
k=0.0454; %W/m.K
Cp=2639.53; %J/kg K
g=0.597; %kg/m3
k2=100000; %mol/m3s.
E2=16000000; %J/kmol
R=8.314; %J/K mol
Pco=0.2;
Ph20=0.8;
Pco2=0;
Ph2=0;
T=633; %K
H=-115823; %J/mol
rco =k2*exp(-E2/(R*T))*((Pco*Ph20-(Pco2*Ph2))/(exp(4577/T)-4.33)); %mol/m3s
I wish you find time out of the tight schedule to solve it.
@@baacademy1166 Are the variables r and z with respect to time?
Dr. Carlos Montalvo yes they are, Unsteady state.
what happens when the condition is steady state ?
Darren Pinto nothing. once the condition reaches steady state the simulation won't change
Is the pdf you’re using open access? I’d like to read it please.
Where would I upload something like that? arXIV? Github?
@@CarlosMontalvo251 Could always try google drive, onedrive, or dropbox all free
@@patcragg7034 Went ahead and just through it on github. github.com/cmontalvo251/MATLAB/blob/master/Montalvo_Numerical_Methods_Textbook.pdf
Perhaps it will help some of you out
@@CarlosMontalvo251 really appreciate it thank you! Often times I find people's personal notes much more comprehensive and useful. Thanks a ton!
clear
clc
close all
%%% Heat Constant - Heat Capacitance
k=2;
%%% Length of the pipe
L = 10;
%%% Number of Elements
N = 10;
%% Discretize our Xspace
x_vec = linspace(0,L,N);
dx = x_vec(2)-x_vec(1);
%% Discretize time
dt = 0.5*dx^2/(2*k);
t_vec = 0:dt:10;
%%% Allocate memory for Temp
T_mat = zeros (length(x_vec),length(t_vec));
%%% Initial Conditions for the Temprature
T_mat(1,:) = 200;
T_mat(end,:) = 150;
%%% Integrate using Euler's and FDM put together
for tdx = 1:length(t_vec)-1
for idx = 2:length(x_vec)-1
T_mat (idx,tdx+1)= T_mat(idx,tdx) + k*dt/dx^2 * (T_mat(idx+1,tdx)-2*T_mat(idx,tdx)+T_mat(idx-1,tdx));
end
end
[tt,xx] = meshgrid(t_vec,x_vec);
mesh(xx,tt,T_mat)
xlabel('X coordinate (m)')
ylabel('Time (sec)')
zlabel('Temperature (F)')
Sir, i m trying to solve three partial differential equation ,where two is linear and one is non-linear. They have three unknown and these equation are related. Can u pliz help in dealing with 3 unknown variables
To solve multiple equations simultaneously you just need to stack them in vector format and integrate them simultaneously. In this tutorial I show how to integrate a 2 state system using RK4. PDE's aren't much different. fastlabtutorials.blogspot.com/2015/03/everything-you-need-to-know-to-code.html?view=classic
Thank you Sir.
Sir, is it posible in matlab to solve three non-linear PDE which are interlinked together??
+Naz Rahman I think so but I've never done it before. my specialty is in ODEs.
Wil you give a try to solve my non-linear PDE if I send you my equation set?
Please can you share us the book you are working
“Numerical Methods for Engineers, 7th Ed.”, S. Chapra and R. Canale, McGraw-Hill, ISBN
9781259318726.
“Essential MATLAB and Octave” J. Rogel-Salazar, CRC Press, 2015. ISBN 9781482234633
Never mind about my question. This is the textbook we actually use at my uni. Thanks.
Sir, just I am asking how can i write a matlab code for Explicit Finite Difference Method for Parabolic PDEs ..... thanks
I do not know how to do that. This is not my immediate area of expertise.
Dude, why are you doing 3 commentary signs?
Because 2 signs creates separate cells in the editor which annoys me and one sign by itself isn't good enough for me. Call it a bad habit if you want.
do you cover the CN method?
CN?
crank nicolson
I covered it when I taught this class but I don't recall how to do that method unfortunately.
please can i have the book that you use in this video.thanks
can i get the code for 2D unsteady flow ?
I do not have the code you're looking for
sir , i m to sovle E field riadition from a dipole antenna in 3D
Hi emad, I am an aerospace engineer. E field radiation is beyond my immediate area of expertise.
What textbook are you using?
Numerical Methods by Chapra
Sorry, I scrolled down in comments and saw someone ask. My bad. Do you have any vids on FEA?
I do not.
uhmmmm good evening i dont speaks english sry :D
If you give me initial temperature where would it go in your program?
Initial conditions are set on line 34 at 6:43
Nono, if it is a bar with 100 F on both sides and the initial temperature is 20F how could it be? uhmmm ty anyway
heey dr.. plz help me .. i want the FdTd 2d programing ..10k's
abdellah cherifi I have this on my to-do list to complete a 2D finite difference method I just haven't done it yet
soo ...
well I should probably learn how to use Matlab in the first place...
rola codigo hermano (drop the code bro)
Leiste el descripcion?
please sir it is very important for me