Use Euler\'s Method for Integration to solve dy/dx = x^2 for y by creating a scr
ID: 3871627 • Letter: U
Question
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.