Euler's, Improved Euler's, and Runge Kutta code

18 visualizaciones (últimos 30 días)
pemkay
pemkay el 10 de Oct. de 2017
Respondida: Jyotish Robin el 13 de Oct. de 2017
I am new to MatLab and I have to create a code for Euler's method, Improved Euler's Method and Runge Kutta with the problem ut=cos(pit)+u(t) with the initial condition u(0)=3 with the time up to 2.0. I need to graph the solution vs. the exact solution, and a graph of the errors for number of points N=10,20,40,80,160,320,640. I have started the Euler's code but I keep getting undefined function or variable y
clear t % Clears old time steps and
clear y % y values from previous runs
a=0; % Initial time
b=2; % Final time
N=[10 20 40 80 160 320 640]; % Number of time steps
y=3; % Initial value y(a)
t(1)=a;% Time step
y0=y;
yt=cos(pit)+y(t);
hold on;
for n=1:length(N)
h=(b-a)/N(n);
for i=1: N(n)
t(i+1)=t(i)+h;
y(i+1)=y(i)+(h*f_problem1(t(i),y(i)));
end
plot (t,y,'*');
end
t=linspace(0,b,100);
title(['Question 1 Euler Method' , name,])
legend('N=10', 'N=20','N=40', 'N=80', 'N=160', 'N=320', 'N=640')

Respuestas (1)

Jyotish Robin
Jyotish Robin el 13 de Oct. de 2017
Hi Pratyusha!
I just had a quick look at your code and there are a few issues that I would like to point out as follows:
yt=cos(pit)+y(t);
  • You cannot use pit , you have to specify as pi*t
  • Since you define t(1)=a (whose value is 0), y(t) is referring to y(0). In MATLAB indexing starts from 1. So y(0) is erroneous.
In general , I would suggest you to look through MATLAB documentation to understand these things better.
For example, https://www.mathworks.com/help/matlab/math/matrix-indexing.html talks about Matrix indexing in MATLAB.
Hope the above suggestions help!
Regards,
Jyotish

Categorías

Más información sobre App Building en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by