- #1
Rachelross
- 4
- 0
Homework Statement
Write a script file to solve the DE dx/dt=1/x^2+tanh(t+1)+C using Euler's method .Solve the equation from t0 to t1 using step size h =0.02 and initial condition x(0)=5.Find the value of x(t1) for values of C from -3 to -1 in steps of 0.1 .plot x(t1) for different values of C
Homework Equations
I managed to write the code ,but I am not too sure how to plot x(t1) for different values of C .Can someone help please?
The Attempt at a Solution
% script file to solve a differential equation using Euler's method
clear all
clc
% set initial conditions, integration limits, and step size
x0 = 5;
t0 = 0;
t1 = 3;
h = 0.02;
%set up an array of t points
t = [t0:h:t1];
N = length(t);
%initialise the x array
x = zeros(1,N); x(1) = x0;
for C=-3:0.1:-1
for I = 2:N
% calculate the function
f = 1/((x(I-1))^2)+tanh(t(I-1)+1)+C;
% Euler's method
x(I) = x(I-1) + h*f;
end
end
final_x=x(N)