Taylor and Euler Matlab Comparison for Numerical Analysis.

In summary, the conversation discusses using Euler and Taylor methods to solve the differential equation y'=3t^2y^2 on [0, 3] with initial condition y0=-1. The Euler method and Taylor method of order 3 are compared to the exact solution, y(t)=(-1/((t^3)+1)). The discussion also includes a request for help with a coding error in the program.
  • #1
acampbell
3
0
1. Solve y'=3t^2y^2 on [0, 3] , y0 = −1, using Euler method and Taylor method of
order 3. Compare your solutions to the exact solution. y(t)=(-1/((t^3)+1))


I DONT KNOW WHAT IS WRONG WITH MY PROGRAM! PLEASE HELP =D

Homework Equations



http://en.wikipedia.org/wiki/Euler_method
http://math.fullerton.edu/mathews/n2003/TaylorDEMod.html

The Attempt at a Solution


function Euler2(a,b,h,y0)
t0=a;
t(1)=t0;
y(1)=y0;
N=(b-a)/h;

for i=1:N
y(i+1)=y(i)+h*(func(t(i),y(i)));
t(i+1)=t(i)+h;
end
-----------------------------------------
function deriv=func(t,y)
deriv=3*t^2*y^2;
-----------------------------------------
function w=Taylor1(a,b,h)
t0=a;
y0=1;
t(1)=t0;
y(1)=y0;
N=(b-a)/h;
w(1)=1;

%2nd Order Taylor Method%
for i=2:N+1
w(i)= w(i-1)+h*(1-h+(h^2)/2);
t(i)= a+i*h;
end

%3rd Order Taylor Method%
for i=2:N+1
w(i)= w(i-1)*(1-h+(h^2)/2-(h^3)/6);
t(i)= a+i*h;
end
------------------------------------------------
a=0;
b=3;
y0=-1;
h=0.1;
t=a:h:b;

w1=Euler2(a,b,h);
w2=Taylor2(a,b,h);

plot(t,w1,'r')
hold on
plot(t,w2,'b')
hold on
plot(t,-1/(t^3+1),'g')
--------------------------------------
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
acampbell said:
1. Solve y'=3t^2y^2 on [0, 3] , y0 = −1, using Euler method and Taylor method of
order 3. Compare your solutions to the exact solution. y(t)=(-1/((t^3)+1))


I DONT KNOW WHAT IS WRONG WITH MY PROGRAM! PLEASE HELP =D

Homework Equations



http://en.wikipedia.org/wiki/Euler_method
http://math.fullerton.edu/mathews/n2003/TaylorDEMod.html

The Attempt at a Solution


function Euler2(a,b,h,y0)
t0=a;
t(1)=t0;
y(1)=y0;
N=(b-a)/h;

for i=1:N
y(i+1)=y(i)+h*(func(t(i),y(i)));
t(i+1)=t(i)+h;
end
-----------------------------------------
function deriv=func(t,y)
deriv=3*t^2*y^2;
-----------------------------------------
function w=Taylor1(a,b,h)
t0=a;
y0=1;
t(1)=t0;
y(1)=y0;
N=(b-a)/h;
w(1)=1;

%2nd Order Taylor Method%
for i=2:N+1
w(i)= w(i-1)+h*(1-h+(h^2)/2);
t(i)= a+i*h;
end

%3rd Order Taylor Method%
for i=2:N+1
w(i)= w(i-1)*(1-h+(h^2)/2-(h^3)/6);
t(i)= a+i*h;
end
------------------------------------------------
a=0;
b=3;
y0=-1;
h=0.1;
t=a:h:b;

w1=Euler2(a,b,h);
w2=Taylor2(a,b,h);

plot(t,w1,'r')
hold on
plot(t,w2,'b')
hold on
plot(t,-1/(t^3+1),'g')
--------------------------------------

Help us out a bit. Why do you think there is something wrong with your code?
 
Last edited by a moderator:
  • #3
i feel like something is wrong with my Euler and Taylor codes but I'm not so sure.
 
  • #4
"Feeling" is not helpful. Does your code produce an incorrect result?
 
  • #5
? Error using ==> Euler2
Too many output arguments.

Error in ==> Problem3_Homework3 at 7
w1=Euler2(a,b,h);


THAT IS WHAT I GET!
 
  • #6
Umm, just looking at your euler's I can tell you either didn't give us the right code or it's wrong (or terrible coding practice). You have a t(1)=t0; definition, but t0 is not an input to your function. Other than that, it looks fine. I don't really want to look at the Taylor without in an indication of whether it's wrong or not.

The two methods should be pretty close to the exact solution.

Edit: my bad, didn't see that you made a = t0, but still, why the extra step?
 
  • #7
Your function call should really look like

function y = Euler2(a,b,h,y0)

Maybe you should make it look just like the ode45 would take
function [t y]=euler(odefun,tvalues,y0,h);
 
Last edited:

1. What is the purpose of the "Taylor and Euler Matlab Comparison for Numerical Analysis" project?

The purpose of this project is to compare and contrast the Taylor and Euler methods for numerical analysis in Matlab. These methods are commonly used for solving differential equations numerically, and this project aims to demonstrate the similarities and differences between them.

2. How are the Taylor and Euler methods different from each other?

The main difference between the two methods is the way they approximate the solutions to differential equations. The Taylor method uses higher-order derivatives to improve accuracy, while the Euler method only uses first-order derivatives. This results in the Taylor method being more accurate but also more computationally intensive.

3. Which method is better for solving differential equations in Matlab?

The answer to this question depends on the specific problem at hand. In general, the Taylor method is more accurate and can handle more complex equations, but it requires more computational resources. The Euler method is less accurate but is faster and simpler to implement. It ultimately comes down to the trade-off between accuracy and efficiency.

4. What are the advantages of using Matlab for numerical analysis?

Matlab is a powerful and widely used software for scientific computing, making it a popular choice for numerical analysis. Some of the advantages of using Matlab for this purpose include its user-friendly interface, built-in functions for solving differential equations, and efficient handling of large datasets. Additionally, Matlab allows for easy visualization of results, making it a valuable tool for analyzing and interpreting numerical data.

5. Are there any limitations to using Matlab for numerical analysis?

While Matlab is a versatile and powerful tool, it does have some limitations for numerical analysis. One limitation is its cost, as it is a proprietary software that requires a license. Another limitation is that it is not open-source, meaning users cannot access or modify the source code. Additionally, Matlab may not be the best choice for solving certain types of differential equations, such as stiff equations, which require specialized methods.

Similar threads

  • Calculus and Beyond Homework Help
Replies
10
Views
382
  • Calculus and Beyond Homework Help
Replies
6
Views
861
  • Calculus and Beyond Homework Help
Replies
3
Views
574
  • Calculus and Beyond Homework Help
Replies
2
Views
163
  • Calculus and Beyond Homework Help
Replies
2
Views
397
  • Calculus and Beyond Homework Help
Replies
1
Views
963
  • Calculus and Beyond Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
829
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Calculus and Beyond Homework Help
Replies
1
Views
708
Back
Top