Question
Matlab question Modify the LaplaceDirichlet.m code to handle mixed boundary conditions on all four sides and rename the code to LaplaceMixed.m. here is the code:
Matlab question
Modify the LaplaceDirichlet.m code to handle mixed boundary conditions on all four sides and rename the code to LaplaceMixed.m.
here is the code:
clear all; close all; clc; %template code that can be modified in various ways to generate numerical %approximations to the solution of most pdes you'll see in this course and %others
%Use Matlab to solve laplace's equation in a box on 0<=x<=1,0<=y<=1 %boundary conditions are zero everywhere except at the top where phi=1, %y=1
%when you run the code the figure will show you that the heat escapes %through the shortest pathway which is right around the top corners
%clear variables and close all figures so no stuff left over between runs clear all;close all; %set shift - this makes our formulas look like they do in the notes shift=1; %box dimensions xmax=1; ymax=1; %set spacing deltax=0.025; %to change deltay from deltax requires a small change in the formula used %below to solve laplace's equation deltay=deltax; %set number of nodes in each direction -- they are the same in this %problem m=fix(1/deltax+shift); n=m; %setup array of zeros phi=zeros(m,n); %set boundary condition at y=ymax for i=1:m phi(i,n)=10; end %set boundary condition at y=0 for i=1:m phi(i,1)=5; end %set boundary condition at x=0 for j=1:n phi(1,j)=5; end %set boundary condition at x=xmax for j=1:n phi(n,j)=-2; end %state error tolerance errmax=0.01; %initialize error level error=-1000.0; %initialize state to NOT done done=1; %initialize total iterates numberofiterates=0; while(done==1) numberofiterates=numberofiterates+1; error=-1000.0; %laplace's equation for i=1+shift:n-1 for j=1+shift:m-1 oldphi=phi(i,j); phi(i,j)=(phi(i+1,j)+phi(i-1,j)+phi(i,j-1)+phi(i,j+1))/4; currenterror=abs((oldphi)-phi(i,j)); if(currenterror>error) error=currenterror; end end end if(error
%let us know what done state was and numberofiterates fprintf(' done state=%d (done=-9 NOT done) numberofiterates=%d',done,numberofiterates); %put values to screen for viewing %orient them so that they appear as they would on a x,y plot fprintf(' ************************ '); fprintf(' Here come the phi values '); fprintf(' ************************ '); for i=m:-1:1 for j=1:n fprintf('%3.5f ',phi(j,i)); end fprintf(' '); end
%set up x,y positions for plotting [X,Y] = meshgrid(0:deltax:xmax, 0:deltay:ymax); phi=transpose(phi);
%see help on contour to follow this bit %note that contourf will fill in the contours if you like the picture to %look that way [C,h] = contourf(X,Y,phi); clabel(C,h); title('Laplace Equation Pretty Picture y=1, \phi=1');
%plot flow of heat -- i.e. the flux is -K gradient(phi) hold on; [fx,fy]=gradient(phi,deltax,deltay); quiver(X,Y,-fx,-fy,3.5,'filled','k');
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