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

  • Thread starter Rachelross
  • Start date
In summary, the conversation is about writing a script file to solve a differential equation using Euler's method, with specific initial conditions and integration limits. The attempt at a solution involves setting up arrays, initializing variables, and using a for loop to calculate the function and plot the result using MATLAB.
  • #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)
 
Physics news on Phys.org
  • #2
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.
 
  • #3
You can also type 'help plot' into the command window to get some basic documentation.
 

What is a DE (differential equation)?

A differential equation is a mathematical equation that describes the relationship between a function and its derivatives. It is commonly used in science and engineering to model various physical phenomena.

What is dx/dt?

dx/dt is the notation used to represent the derivative of a function x with respect to the variable t. It represents the rate of change of the function with respect to time.

What is Euler's Method?

Euler's Method is a numerical method for solving differential equations. It involves approximating the solution by using small steps and a tangent line to estimate the next value of the function. It is an iterative process that can provide a close approximation to the actual solution.

How do you solve a DE using Euler's Method?

To solve a differential equation using Euler's Method, you need to first rewrite the equation in the form of dx/dt = f(x,t), where f(x,t) is a function of x and t. Then, choose a starting value for x and a step size h. Use the formula x(n+1) = x(n) + hf(x(n),t(n)) to calculate the next value of x, where n represents the current step. Repeat this process until you reach the desired number of steps or a specific endpoint.

What is the role of the constant C in the given differential equation?

The constant C is known as the constant of integration and is added to the solution of the differential equation to account for any initial conditions or unknown factors. It can be determined by using the given initial conditions or by solving for it using other methods.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
916
  • Calculus and Beyond Homework Help
Replies
9
Views
658
  • Precalculus Mathematics Homework Help
Replies
7
Views
765
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Differential Equations
Replies
1
Views
717
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top