Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use Euler\'s Method for Integration to solve dy/dx = x^2 for y by creating a scr

ID: 3871627 • Letter: U

Question


Use Euler's Method for Integration to solve dy/dx = x^2 for y by creating a script file in Matlab. First, plan your code by writing it by hand. Then, implement in Matlab. Begin by developing a for loop to calculate y (much as we've done before). The range of x values is x = 0 to 3, and the initial Delta x stepsize to be used is 0.5. Be sure to use variables for Delta x, x initial, and x final. Next implement a while loop to rerun your calculations (this means the for loop is inside the while loop) for smaller and smaller Delta x values (reduce the Delta x value in half for every iteration). The while loop should run until the percent error (in decimal form) between your numerical solution (at x = 3) and the exact analytical solution (at x = 3) is less than 0.01 (1%). Plot every iteration of your y estimate (using dashed lines) and the exact y solution (using a continuous line) on the same plot. Report the percent error values in the command window.

Explanation / Answer

Step1:

Create a file 'f.m' . It will contain the defination of the function eg. defrential equation

%-----f.m--file------

function f=f(t,y)
f=3+t-y; % Define the derivetive of the function here. Replace this line with your function

step 2:

Create a file 'yEexact.m' it will conatin the exact solution of you function

function exact_value=yEexact(x)
exact_value = ; % Exact solution of y here

Step 3:

xi=0; % starting value of x
xf=3; % Final value of xf

df=0.5; % stepsize
N=(xf-xi)/dx; % number of for loop
y0=0; % Initial value y(x)

x(1)=xi;
y(1)=y0;
yexact=yEexact(3);
er=2000; % error initialised to very larg value. It depend on your function
while(er>0.01)
for n=1:N % For loop, sets next x,y values
x(n+1)=x(n)+h;
y(n+1)=y(n)+h*f(x(n),y(n)); % Calls the function f(x,y)=dy/dx
end
yexact=yEexact(3);
er=abs(yexact-y(N)); % error calculation
N = (xi-xf)/(dx/2); reduce the dx to half and calculate new value for number of loop
end

figure

plot(x,y); % plot extimate value

figure

plot(x,yexact); % plot exact value

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote