code for HW4

# code for HW4 - function[x,y]=Shooting(dx Initial guess v2=0...

• Homework Help
• 2
• 100% (2) 2 out of 2 people found this document helpful

This preview shows pages 1–2. Sign up to view the full content.

function [x,y]=Shooting(dx) %% Initial guess v2=0; % Shooting method it=1;itmax=50; crit1=inf; crit2=inf; tol=10e-12; % Initial conditions a1=1;a2=1;b1=0;b2=0;a=0;b=0; c0=[0;v2;0;1]; while crit1>tol && crit2>tol && it<itmax cin=c0; [x,y]=RK4(dx,cin); R=a2*y(1,end)+b2*y(2,end)-b; Rq=a2*y(3,end)+b2*y(4,end); v2new=v2-R/Rq; crit1=norm(v2new-v2); crit2=norm(R); fprintf( '%3d % 20.14f %20.14f %10.6e %10.6e % 20.14f \n ' , it,v2,v2new,crit1,crit2,y(1,end)); v2=v2new; c0=[0;v2;0;1]; it=it+1; end %% Compute the Analytical Solution T=300; E=7*10^10; I=4; L=50; theta=sqrt(T/(E*I)); w=50; C=w/theta^2/T/sinh(theta*L); yexact=C*sinh(theta*x)+C*sinh(theta*(L-x))+w*L*x/2/T-w/theta^2/T-w*x.^2/2/T; %% Plot the numerical y(x) and analytical yexact(x) solutions figure(1),clf plot(x,y(1,:), 'bx' , 'markersize' ,12),hold on plot(x,yexact', 'ro' , 'markersize' ,5) set(gca, 'fontsize' ,12) xlabel( 'x' , 'fontsize' ,12,

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: , 'bold' ) ylabel( 'y(x)' , 'fontsize' ,12, 'fontweight' , 'bold' ) legend([ 'Shooting method dx = ' num2str(dx) ')' ], 'Analytical' ,1) title( 'Solution of the BVP' , 'fontsize' ,12, 'fontweight' , 'bold' ) function dy=yprime(x,y) dy=zeros(4,1); dy(1)=y(2); dy(2)=(300)*y(1)/(7*10^10)/4+50*x*(x-50)/2/(7*10^10)/4; dy(3)=y(4); dy(4)=(300)*y(3)/(7*10^10)/4; end function [x,y]=RK4(dx,cin) xspan=[0 50]; x0=xspan(1); xfinal=xspan(end); x=[x0:dx:xfinal]; if x(end)~=xfinal x=[x xfinal]; end n=length(x); y=zeros(4,n); y(:,1)=cin; for N=1:n-1 k1=yprime(x(:,N),y(:,N)); k2=yprime(x(:,N)+(dx/2),y(:,N)+(dx/2)*k1); k3=yprime(x(:,N)+(dx/2),y(:,N)+(dx/2)*k2); k4=yprime(x(:,N)+dx,y(:,N)+(dx*k3)); y(:,N+1)=y(:,N)+((1/6)*dx*(k1+(2*(k2+k3))+k4)); end end clear all ;clc dx=1; [x,y]=Shooting(dx);...
View Full Document

• Spring '14
• Boundary value problem, x,y, BVP

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern