Solving DE dx/dt=1/x^2+tanh(t+1)+C w/Euler's Method

  • Thread starter Thread starter Rachelross
  • Start date Start date
AI Thread Summary
The discussion focuses on solving the differential equation dx/dt=1/x^2+tanh(t+1)+C using Euler's method, with specific parameters including an initial condition of x(0)=5 and a step size of h=0.02. A script has been developed to compute x(t1) for values of C ranging from -3 to -1 in increments of 0.1. The main challenge is plotting x(t1) for these different values of C in MATLAB. Guidance is provided on defining the domain for x and using the plot function effectively. The conversation emphasizes utilizing MATLAB documentation for further assistance with plotting syntax.
Rachelross
Messages
3
Reaction score
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)
 
Physics news on Phys.org
To plot in MATLAB you must define your 'x' as a domain (if you don't want to graph just one point..). Then you have your actual function, 'y'. The plot function is simply plot(x,y), and the domain for x can be set like this: x = beginning:increment:end; with all punctuation included.

You ought to use the documentation on the MathWorks website, simply google 'plot matlab' and it will take you there. It's a good tool for future questions you may have as far as syntax goes.
 
You can also type 'help plot' into the command window to get some basic documentation.
 
Back
Top