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

  • Thread starter Thread starter Rachelross
  • Start date Start date
Click For Summary
SUMMARY

This discussion focuses on solving the differential equation dx/dt=1/x^2+tanh(t+1)+C using Euler's method in MATLAB. The script provided initializes conditions with x(0)=5, integrates from t0=0 to t1=3 with a step size of h=0.02, and iterates over values of C from -3 to -1 in increments of 0.1. The user seeks assistance in plotting the resulting x(t1) values for different C values, with guidance provided on MATLAB's plotting functions.

PREREQUISITES
  • Understanding of differential equations and initial value problems
  • Familiarity with MATLAB programming and script writing
  • Knowledge of Euler's method for numerical integration
  • Basic skills in data visualization using MATLAB's plotting functions
NEXT STEPS
  • Learn MATLAB plotting techniques, specifically using the 'plot' function
  • Explore MATLAB documentation on numerical methods for solving differential equations
  • Investigate the effects of varying step sizes in Euler's method
  • Study the implications of different initial conditions on the solution of differential equations
USEFUL FOR

Students and educators in mathematics or engineering fields, MATLAB users interested in numerical methods, and anyone looking to visualize solutions to differential equations.

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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
5K
Replies
9
Views
3K