Question
Just need help with hand in portion 3. This is done in matLab Math 253, Fall 2021 - Lab 2. Printed hand-in portion due Monday
Just need help with hand in portion 3. This is done in matLab
Math 253, Fall 2021 - Lab 2. Printed hand-in portion due Monday Nov 22 in class. 1. Make sure you have a directory somewhere from last lab where you store your matlab files. So were all starting off correctly, go to our moodle site and download the diffusionmain.m file, and put it in your directory. This is the program from last lab. Make sure Matlab can find your directory (as we ironed out last lab using Set Path). 2. Load up Matlab on your computer. 3. in the Command Window, type diffusionmain and verify that the program is running (Youll see a plot window with the diffusing particles moving about). 4. If its not working, click on Home / Set Path, and add in your directory. 5. Next, type edit diffusionmain and itll pop up in the script editor. (Or, Home / Open / ...) Advection If particles diffuse within a moving fluid, they are additionally advected by the fluid in the direction of fluid motion. We can add this effect in very simply to our diffusion program. Modify the xnew and ynew lines in the for loop by adding on 0.01 to their position at each time step. This moves each particle a fixed distance each time step, in addition to diffusing. Thus, change these lines to xnew = 0.01+x + k.*cos(randdirection); ynew = 0.01+y + k.*sin(randdirection); Then save, and run the program, and observe. Try changing the numbers and see how that changes the motion of the particles. HAND IN # 1 With 1000 particles and for 500 timesteps, make the particle blob diffuse (at the same speed) to the lower left, instead of upper right. Save the image at the end (time=500) and put in your writeup. Write a sentence or two indicating how you modified the code to achieve this. For this (and every other) plot that you save for your writeup, include with it a title with your name via the line title([your name]: Plot title) changing the name and text to suit you and your specific plot. Diffusion in Heterogeneous Environments. Often, particles diffuse in environments which have different features in space: think of tiny organisms in fluids of varying viscosity, particles on surfaces with varying friction, or even (more abstractly) people moving in a city via road transit versus walking. One way to represent the different features is to change the diffusion coefficient, depending on the location of a particle. Lets do this! 1 1. Go to diffusionmain.m, and remove the advection you added above (so just diffusion again). 2. In the Editor window, click New (drop down)... then Function. A new tab should open up called untitled. Change the preset function definition (input args, etc) to function k = coeff(x,y) Click Save As... and save it as coeff.m in your MATLAB desktop directory. Here, the x and y are inputs to the function, and get passed in. The function will pass back whatever k is calculated to be within the function. 3. (test to see if its set up correctly) Between the function and end, write k = 0.2; 4. (test to see if its set up correctly) Now, back in the diffusionmain.m editor window, update the position calculation lines in the for loop to xnew = x + coeff(xnew,ynew).*cos(randdirection); ynew = y + coeff(xnew,ynew).*sin(randdirection); Now, your diffusionmain program will call the coeff.m function for each timestep in the for loop. Save your changes, and re-run diffusionmain in the command window. Make sure it works (particles should diffuse normally). (Note: in this test, the diffusion coefficient doesnt change based on position... yet!) 5. Once working, go back to the coeff function in the Editor, and change the expression for k: k = 0.2*(x>=y) + 0.05.*(x y, and 0.05 for all locations where x < y. In general, we can use this to define any areas: HAND IN # 2 Use k below (this sets the diffusion coefficient to 0.25 inside a square from -5 to 5, and 0.05 outside that square), and run the program for 10000 particles for 1000 timesteps. Save the figure image of the particles once the simulation is finished, and include in your writeup, commenting on the effect observed. k = 0.2*(x -5 & y > -5 & y<5 ) + 0.05; Make sure the axes window is the proper size. HAND IN # 3 Write an expression for k in coeff that sets the diffusion coefficient to be 0.15 for any particles inside the circle of radius 5 centred on the origin, and 0.05 outside (hint: think of the equation of a circle x 2+y 2 = r 2 . Then inside the circle is where x 2+y 2 < r2 , for instance). Running for 10000 particles and 1000 timesteps, save the figure image once the simulation is finished and include in your writeup.
The base
hold off nop = 1000; totaltime=500; x = zeros(nop,1); y = zeros(nop,1); xnew=x; ynew=y; k=0.1; meandist = zeros(totaltime,1); for i = 1:totaltime randdirection = 2*pi*rand(nop,1); xnew =x + k.*cos(randdirection); ynew =y + k.*sin(randdirection); hold off; plot(xnew, ynew, '.'); hold on plot(xnew(30),ynew(30),'ok','LineWidth',2); axis([-5 5 -5 5]); title('[your name]: [Plot title]') axis square; pause(0.03); x = xnew; y = ynew; i;
end
function k = coeff(x,y)
k=????
end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started