.

Differential equation in matlab

First order Differential Equations in Matlab


solve the following 2nd order differential equation using euler's numerical methods, Matlab solve differential equation numerical methods

y'=y

by the initial conditions;

y(0)=1

MATLAB code is as follows:

		tn = 4;
		Nt = 50;
		dt = tn/Nt;
		t = 0:dt:tn;

		x0 = 1;  % Initial values for x 

				x = zeros(Nt+1,1);

				x(1) = x0;

				for n = 1:Nt;

			x(n+1) = x(n) + dt*x(n);

		end

		plot(t,x,'k');  % Plotting solved values
		legend('x(t)');
 

t vs x plot is as follows:

First Order Differential equation in matlab

2nd order Differential Equations in Matlab


solve the following 2nd order differential equation using euler's numerical methods, matlab solve differential equation numerically

x¨=−x+sin(t)

by the initial conditions;

x(0)=1
x˙(0)=1

MATLAB code is as follows:

		tn = 4;
		Nt = 100;
		dt = tn/Nt;
		t = 0:dt:tn;

		x0 = 1;  % Initial values for x 
		u0 = 1;  % Initial values for x˙

		x = zeros(Nt+1,1);
		u = x;

		x(1) = x0;
		u(1) = u0;

		for n = 1:Nt;

			u(n+1) = u(n) + dt*(-x(n)+sin(t(n))); % Solving Differential Equation
			x(n+1) = x(n) + dt*u(n);

		end

		plot(t,x,t,u,'k');  % Plotting solved values
		legend('x(t)','u(t)');
 

t vs x and t vs dx/dt plot is as follows:

2nd Order Differential equation in matlab
.
citation generator
citaion generator
make money online