Question

New folder Bookmarks G Google dy/dx x+y, y(0)=1 2 h Exact Solution 1.8 Approximate Solution Mesh Points 1.6 -Direction Fied 1
Project 1 PDF-Summer 2019-x A 2019 La HD 16 aälall JW) x MATLAB Functions nt id 1160071 18course id 29592 1 EDE Consider the
Jsersleskandara\Desktop\New folder (7leuler.m* PUBLI.. VIEW 2 VIGATE EDIT BREAKPOINTS RUN function [xval, yval] euler (f,a,b,
the code in the photo for this I.V.P
dy/dx= x+y. y(0)=1
i need the two in the photo
thank you
New folder Bookmarks G Google dy/dx x+y, y(0)=1 2 h Exact Solution 1.8 Approximate Solution Mesh Points 1.6 -Direction Fied 1.4 1.2 1 0.8 04 0.2 0.3 0.1 0 X CAUsersleskandara\Desktop\New folder emo.m EDITOR PUBLISH VEW Run Section FILE NAVIGATE EDIT Breakpoints Run Run and FL Advance Run and Advance Time BREAKPOINTS RUN 1 - clear all 2 clc 3- clf 4 - close all S - format short 4 decimal places format long for 16 decimal places f@(x, y) x+y: RHS of ODE yexact @(x) 2 exp (x)-x-1: 4You may or may not know this 6- 7- 8- a 0: Left endpoint aka x0 b 0.4: ARight endpoint aka xn 10 h-0.1; Step Size 11 yo 40; Initial Value xVec-a:0.001:b: Vector to smooth out the graph of yexact 12 13 [xVal, yVal] euler (f, a, b, yo, h): Generating the mesh points absErr abs (yVal -yexact (xVal)); Absolute error disp ( disp ([xVal yVal yexact (xVal) absErr]) Printing Everything 14 15 xn yn yexact abs error') 16 17 hold all 18 plot (xVec, yexact (xVec)) AExact Solution curve plot (xVal, yVal) Approximate solution curve scatter (xVal, yVal, o') Mesh points dirfield (f, a: 0.01 : b, 0.75:0.05:2) Direction Field 19 20 21 22- xlabel ('x') 23 ylabel ('y') title ('dy/dx-x+y, legend ('Exact Solution', 'Approximate Solution','Mesh Points',Dire 24- y (0)-1' ) 25 script n er o
Project 1 PDF-Summer 2019-x A 2019 La HD 16 aälall JW) x MATLAB Functions nt id 1160071 18course id 29592 1 EDE Consider the following two initial value problems along with their exact solutions FILE dy 50e-10z dr 2y, y(0) 40, re [0,2 185 -2 25 -10 y= 4 e 4 and dy cos(T) 2y, y(0) = 2, re [0,6 (6+2m2)e- y= - 2r +T sin( Tx ) +2 cos (Tz) 10 4+2 11 - 1. For each I.V.P. you should write a MATLAB script file to solve the problem ing Euler's method with the following four values of h: h 0.1, 0.01, 0.001, 0.00001 us- 2. Your code should generate a single plot of the four approximate solution curves, the exact solution and the direction field. Be sure to label the axes, the legend and the title to exactly match the information relating to each problem. a table of values including columns for r, ya, yexact corresponding only to h 0.1. The columns must be 3. In addition, please create and the absolute error labeled. printout of your code, both graphs and both tables. You will submit a *It might be easiest to embed everything into a word document for submission
Jsersleskandara\Desktop\New folder (7leuler.m* PUBLI.. VIEW 2 VIGATE EDIT BREAKPOINTS RUN function [xval, yval] euler (f,a,b, y0, h) = floor (b-a) /h +1; snumber of mesh points = (N, 1) creating an empty vector zeros (N, 1); another empty vector xval = zeros yval xval (1) = a: yval (1) y0; Efor i-2:N a+ (i-1) *h; % mesh points xval (i) (i-1) +h*f (xval (i-1), yval (i-1)) yval (i) yval sapproximate end Col 4 Ln 11 euler 14
0 0
Add a comment Improve this question Transcribed image text
Answer #1


Matlab code for slope field of ode clear all close all %function 1 for ode f-e (x,y) 50*exp(-10*x)-2*y; Direction field 0:5:4 question b xx-linspace (0,6,100) %function for exact solution (6+2 *pi*2) exp (-2*x) +pisin ( pi *x ) +2 * cos ( pi*x)) /(4+ph xn yn y ext 0.10000 2.0000 0.594571 0.847098 0.847098 0.01000 2.0000 0.820517 0.847098 0.00100 2.0000 0.844427 0.00001 2.00Euler method solution for 2nd function -Slope feld Exact Sitn Approx Sitn 0.5 Published with MATLAB R2018a 4

%%Matlab code for slope field of ode
clear all
close all
%function 1 for ode
f=@(x,y) 50*exp(-10*x)-2*y;

%Direction field
x=0:0.25:2; y=0:5:40;
[X,Y]=meshgrid(x,y);
dX=ones(size(X));
dY=50*exp(-10*X)-2*Y;
figure(1)
quiver(X,Y,dX,dY)
axis tight
hold on

xx=linspace(0,2,100);
%function for exact solution
y_ext=@(x) (185/4)*exp(-2*x)-(25/4)*exp(-10*x);

yy_ext=y_ext(xx);
figure(1)
plot(xx,yy_ext,'ko-','linewidth',2);
hold on
fprintf('\n\nEuler solution for 1st function\n')
fprintf('\th\txn\tyn\ty_ext\n\n')
hh=[0.1 0.01 0.001 0.00001];
for j=1:length(hh)
    h=hh(j);
    [x_elr,y_elr1]=euler(f,[0 2],40,h);
    plot(x_elr,y_elr1,'r','linewidth',2);
  
    fprintf('\t%.5f \t%.4f \t%f \t%f\n',h,x_elr(end),y_elr1(end),y_ext(x_elr(end)))
end

legend('Slope field','Exact Sltn','Approx. Sltn','location','northeast')
title('Euler method solution for 1st function')

%function 2 for ode
f=@(x,y) cos(pi*x)-2*y;

%Direction field
x=0:0.25:6; y=0:.5:3;
[X,Y]=meshgrid(x,y);
dX=ones(size(X));
dY=cos(pi*X)-2*Y;
figure(2)
quiver(X,Y,dX,dY)
axis tight
hold on

%question b.
xx=linspace(0,6,100);
%function for exact solution
y_ext=@(x) ((6+2*pi^2)*exp(-2*x)+pi*sin(pi*x)+2*cos(pi*x))/(4+pi^2);

yy_ext=y_ext(xx);
figure(2)
plot(xx,yy_ext,'ko-','linewidth',2);
hold on
fprintf('\n\nEuler solution for 2nd function\n')
fprintf('\th\txn\tyn\ty_ext\n\n')
hh=[0.1 0.01 0.001 0.00001];
for j=1:length(hh)
    h=hh(j);
    [x_elr,y_elr1]=euler(f,[0 6],2,h);
    plot(x_elr,y_elr1,'r','linewidth',2);
  
    fprintf('\t%.5f \t%.4f \t%f \t%f\n',h,x_elr(end),y_elr1(end),y_ext(x_elr(end)))
end

legend('Slope field','Exact Sltn','Approx. Sltn','location','northeast')
title('Euler method solution for 2nd function')


%Function for Euler ode solution
%Euler method
function [x,y]=euler(f,xx,y_in,h)
    %function (i)
    %f=@(x,y) 50*exp(-10*x)-2*y;
    %initial and final x
    %x_in=0; x_end=2;
    %y_in=40;
    x_in=xx(1); x_end=xx(2);
    %initial conditions
    x(1)=x_in; y(1)=y_in;        
    x_max=x_end;       %Final x
    N=(x_max-x_in)/h; %number of steps
    %Euler iterations
    for i=1:N
      
        x(i+1)=x_in+i*h;
        y(i+1)=double(y(i)+h*(f(x(i),y(i))));

    end
end

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

Add a comment
Know the answer?
Add Answer to:
the code in the photo for this I.V.P dy/dx= x+y. y(0)=1 i need the two in the photo thank you New folder Bookmark...
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