Question

Set up and solve a boundary value problem using the shooting method using MatlabA heated rod with a uniform heat source may be modeled with Poisson equation. The boundary conditions are T(x = 0) = 40 and T

0 0
Add a comment Improve this question Transcribed image text
Answer #1


๖.co) = 40Matlab code using RK4 for 2nd order differential equation clear all close all %Program for RK4 for question a.. f-e(x,yl,y2)h-0.01 xin-x ( 1 ) ; x, max= 10; n= (x max-x-in) /h; %step length %initial x - %Final x %number of steps iterations %Runge KuPlotting of TA(x) using shooting method 150 100 150 0 2345 678 910 Plotting of TB(x) using shooting method 200 150 100 50 -50

%%Matlab code using RK4 for 2nd order differential equation
clear all
close all
%Program for RK4 for question a..

f=@(x,y1,y2) y2;            %function (i)
g=@(x,y1,y2) 25;            %function (ii)
%all guesses for p using shooting method
zg=linspace(-200,100,1000);
for ii=1:length(zg)
    x(1)=0;y1(1)=40;y2(1)=zg(ii); %initial conditions
    h=0.01;                 %step length
    x_in=x(1);               %Initial x
    x_max=10;           %Final x
    n=(x_max-x_in)/h; %number of steps

    %Runge Kutta 4 iterations
    for i=1:n

        k0=h*f(x(i),y1(i),y2(i));
        l0=h*g(x(i),y1(i),y2(i));
        k1=h*f(x(i)+(1/2)*h,y1(i)+(1/2)*k0,y2(i)+(1/2)*l0);
        l1=h*g(x(i)+(1/2)*h,y1(i)+(1/2)*k0,y2(i)+(1/2)*l0);
        k2=h*f(x(i)+(1/2)*h,y1(i)+(1/2)*k1,y2(i)+(1/2)*l1);
        l2=h*g(x(i)+(1/2)*h,y1(i)+(1/2)*k1,y2(i)+(1/2)*l1);
        k3=h*f(x(i)+h,y1(i)+k2,y2(i)+l2);
        l3=h*g(x(i)+h,y1(i)+k2,y2(i)+l2);
        x(i+1)=x_in+i*h;
        y1(i+1)=double(y1(i)+(1/6)*(k0+2*k1+2*k2+k3));
        y2(i+1)=double(y2(i)+(1/6)*(l0+2*l1+2*l2+l3));
    end
    yy1(ii)=y1(end);
  
end
p = interp1(yy1,zg,200);

clear x; clear y1; clear y2
%solution using value of p

    x(1)=0;y1(1)=40;y2(1)=p; %initial conditions
    h=0.01;                 %step length
    x_in=x(1);               %Initial x
    x_max=10;           %Final x
    n=(x_max-x_in)/h; %number of steps

    %Runge Kutta 4 iterations
    for i=1:n

        k0=h*f(x(i),y1(i),y2(i));
        l0=h*g(x(i),y1(i),y2(i));
        k1=h*f(x(i)+(1/2)*h,y1(i)+(1/2)*k0,y2(i)+(1/2)*l0);
        l1=h*g(x(i)+(1/2)*h,y1(i)+(1/2)*k0,y2(i)+(1/2)*l0);
        k2=h*f(x(i)+(1/2)*h,y1(i)+(1/2)*k1,y2(i)+(1/2)*l1);
        l2=h*g(x(i)+(1/2)*h,y1(i)+(1/2)*k1,y2(i)+(1/2)*l1);
        k3=h*f(x(i)+h,y1(i)+k2,y2(i)+l2);
        l3=h*g(x(i)+h,y1(i)+k2,y2(i)+l2);
        x(i+1)=x_in+i*h;
        y1(i+1)=double(y1(i)+(1/6)*(k0+2*k1+2*k2+k3));
        y2(i+1)=double(y2(i)+(1/6)*(l0+2*l1+2*l2+l3));
    end
    figure(1)
    %plotting the solution
    plot(x,y1)
    title('Plotting of TA(x) using shooting method')
    xlabel('x')
    ylabel('TA(x)')
  
clear x; clear y1; clear y2;

%Program for RK4 for question b..

f=@(x,y1,y2) y2;            %function (i)
g=@(x,y1,y2) 0.12.*x.^3-2.4.*x.^2+12.*x;       
%all guesses for p using shooting method
zg=linspace(-200,100,1000);
for ii=1:length(zg)
    x(1)=0;y1(1)=40;y2(1)=zg(ii); %initial conditions
    h=0.01;                 %step length
    x_in=x(1);               %Initial x
    x_max=10;           %Final x
    n=(x_max-x_in)/h; %number of steps

    %Runge Kutta 4 iterations
    for i=1:n

        k0=h*f(x(i),y1(i),y2(i));
        l0=h*g(x(i),y1(i),y2(i));
        k1=h*f(x(i)+(1/2)*h,y1(i)+(1/2)*k0,y2(i)+(1/2)*l0);
        l1=h*g(x(i)+(1/2)*h,y1(i)+(1/2)*k0,y2(i)+(1/2)*l0);
        k2=h*f(x(i)+(1/2)*h,y1(i)+(1/2)*k1,y2(i)+(1/2)*l1);
        l2=h*g(x(i)+(1/2)*h,y1(i)+(1/2)*k1,y2(i)+(1/2)*l1);
        k3=h*f(x(i)+h,y1(i)+k2,y2(i)+l2);
        l3=h*g(x(i)+h,y1(i)+k2,y2(i)+l2);
        x(i+1)=x_in+i*h;
        y1(i+1)=double(y1(i)+(1/6)*(k0+2*k1+2*k2+k3));
        y2(i+1)=double(y2(i)+(1/6)*(l0+2*l1+2*l2+l3));
    end
    yy1(ii)=y1(end);
  
end
p = interp1(yy1,zg,200);

clear x; clear y1; clear y2
%solution using value of p

    x(1)=0;y1(1)=40;y2(1)=p; %initial conditions
    h=0.01;                 %step length
    x_in=x(1);               %Initial x
    x_max=10;           %Final x
    n=(x_max-x_in)/h; %number of steps

    %Runge Kutta 4 iterations
    for i=1:n

        k0=h*f(x(i),y1(i),y2(i));
        l0=h*g(x(i),y1(i),y2(i));
        k1=h*f(x(i)+(1/2)*h,y1(i)+(1/2)*k0,y2(i)+(1/2)*l0);
        l1=h*g(x(i)+(1/2)*h,y1(i)+(1/2)*k0,y2(i)+(1/2)*l0);
        k2=h*f(x(i)+(1/2)*h,y1(i)+(1/2)*k1,y2(i)+(1/2)*l1);
        l2=h*g(x(i)+(1/2)*h,y1(i)+(1/2)*k1,y2(i)+(1/2)*l1);
        k3=h*f(x(i)+h,y1(i)+k2,y2(i)+l2);
        l3=h*g(x(i)+h,y1(i)+k2,y2(i)+l2);
        x(i+1)=x_in+i*h;
        y1(i+1)=double(y1(i)+(1/6)*(k0+2*k1+2*k2+k3));
        y2(i+1)=double(y2(i)+(1/6)*(l0+2*l1+2*l2+l3));
    end
    figure(2)
    %plotting the solution
    plot(x,y1)
    title('Plotting of TB(x) using shooting method')
    xlabel('x')
    ylabel('TB(x)')

%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
Set up and solve a boundary value problem using the shooting method using Matlab
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT