135k views
2 votes
Consider the following equation: dy/dt = −20y+7e⁻⁰∙⁵ Given that the value of y=5 when t=0, a) Use the Euler's method to find y for t=0 to 0.1 using step size of Δt=0.01,0.001 and 0.0001 . Write a MATLAB code to do the calculations and show the results as a plot.

User Jmounim
by
8.8k points

1 Answer

6 votes

Answer:

Sure! Here's a MATLAB code that implements Euler's method to solve the given differential equation and plot the results for different step sizes.

```matlab

% Parameters

t0 = 0; % initial time

y0 = 5; % initial value of y

h_vals = [0.01, 0.001, 0.0001]; % step sizes

% Solve the differential equation using Euler's method

for h = h_vals

t = t0:h:0.1; % time points

y = zeros(size(t)); % initialize y values

y(1) = y0; % initial condition

% Euler's method

for i = 1:length(t)-1

dydt = -20*y(i) + 7*exp(-0.5);

y(i+1) = y(i) + h*dydt;

end

% Plot the results

plot(t, y, 'DisplayName', sprintf('\\Delta t = %.4f', h)); hold on

end

% Add labels and legend

xlabel('t')

ylabel('y')

legend('Location', 'best')

```

This code will generate a plot showing the results for step sizes of 0.01, 0.001, and 0.0001.

Note: In the code, the term `exp(-0.5)` is used instead of `e^(-0.5)` to calculate the exponential function.

User Jxx
by
8.6k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.

9.4m questions

12.2m answers

Categories