MATLAB Answers

Find the solution to the differential equation

1 view (last 30 days)
I have been given the fuction dy/dx = 1.5x^2 -2x +3, when y(0) = 1
I have to plot the result and use polyfit to calculate the coefficient of the result.
% THis is what I have so far.
% dn/dt = r*N, N(0) = 1000;
% N(t) = 1.5x^2 -2x +3;
clf; clear all;
h = 0.01 % step
%r = 0.8;
a =-10; % lower
b = 10; %upper
m = (b -a)/h;
N(1)= 1000;
t = a:h:b
for i = 1:m
N(i+1) = N(i) + a*h*N(i);
end
Nex = N(1)*exp(a*t);
disp([t' N' Nex'])
  2 Comments
Cesar Hernandez Reyes
Cesar Hernandez Reyes on 16 Dec 2019
My bad got the wrong file.
I want to find the points without using ode45().
(Should have specified. My bad)
% N(t) = 1.5x^2 -2x +3;
clf; clear all;
h = 0.01 % step
%r = 0.8;
a =-10; % lower
b = 10; %upper
m = (b -a)/h;
N(0)=1 % have been using N(1)=1 since MATlab has not accept N(0)
t = a:h:b
for i = 1:m
N(i+1) = N(i) + h*N(i);
end
Nex = N(1)*exp(a*t);
disp([t' N' Nex'])

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Dec 2019
N(t) = 1.5x^2 -2x +3
That equation does not reflect your original question dy/dx = 1.5x^2 -2x +3 . The difference is in the variable: N(t) makes it a function of time with a constant value (1.5x^2 -2x +3 is constant in t), whereas dy/dx = 1.5x^2 -2x +3 has the output depending on the input x.
You need a couple of values:
  • a vector of Y values indexed by iteration number. This is not exactly the same as the y of dy/dx because the y of dy/dx is a function and y(K) would mathematically indicate y applied to K rather than y indexed at K
  • a vector of X values indexed by iteration number. Again, not exactly the same as the x of dy/dx for the same reason
  • A current iteration number to index into the arrays
  • an initial x value
  • a final x value
At any one iteration after the first, increase the "current" X value by a constant (your h). Calculate dy/dx at that current X value. Use that result and the previous "current" Y value, and the timestep, to interpolate the new "current" Y value. The interpolation formula to use depends upon whether you are using ODE 4/5 or ODE 2/3 or other interpolation method.
If you compare this to what you have, you are missing the calculation of the current dy/dx.
The exponential formula you calculate might be useful for some kind of comparison, but is otherwise unrelated to the problem.
When you get around to estimating the polynomial, what you should get is the integral of 1.5x^2 -2x +3 -- that is, 1/2 * x^3 - x^2 + 3*x + c for some constant c.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by