Borrar filtros
Borrar filtros

Taylor's method of order 2 programming

1 visualización (últimos 30 días)
ivordes greenleaf
ivordes greenleaf el 13 de Abr. de 2015
Comentada: Star Strider el 13 de Abr. de 2015
So I am writing a program to solve an IVP problem, I'm trying to write it with not using the function script because I want to plot the error later.
This is my coding:
clear;
h=0.1; t=1:h:2; n=length(t);
%initial condition y=zeros(n,1); y(1)=-0.5;
for i = 1:n-1 y(i+1)=y(i)+h*RHS(t(i),y(i))+(h.^2/2)*RHS3(t(i),y(i)); end
truesol= (sqrt(3)/(2*t))*tan((sqrt(3)/2)*log(t))-1/2*t;
plot(t,y,'.',t,truesol,'*')
[t' y truesol']
And I wrote my RHS(which is my original function) and RHS3(which is my df/dt) separately:
function result = RHS(t,y)
result= y.^2+(1/(t.^2));
end
and
function result= RHS3(t,y)
result = 2*y.^3+((2*y)/(t.^2))-(2/(t.^3));
end
Everything seems to be okay, I even tried a simple "true solution" and it works. It only doesn't work when I tried the correct true solution, which is
truesol= (sqrt(3)/(2*t))*tan((sqrt(3)/2)*log(t))-1/2*t;
it gives me an error of "??? Error using ==> mldivide Matrix dimensions must agree."
Thanks for any input.

Respuesta aceptada

Star Strider
Star Strider el 13 de Abr. de 2015
See if vectorising this line (replacing (/) with (./) and (*) with (.*)) solves the problem:
truesol= (sqrt(3)./(2*t)).*tan((sqrt(3)/2)*log(t))-1/2*t;
Consider vectorising other lines in your code as well. See Array vs. Matrix Operations for details.
  2 comentarios
ivordes greenleaf
ivordes greenleaf el 13 de Abr. de 2015
Thank you so much, that really does help.
I also want to plot the logarithm of error log|yexact-yapprox| versus the logarithm of step size h, and then find the slope of that straight line through the graph. I wrote a line of code stated
err(i) = (abs(truesol(i)-y(i)));
with
truesol= (sqrt(3)*tan((sqrt(3)/2)*log(t))-1)./(2*t);
and then can I do a plot(loglog(h,err)), it does give me something, and it's a graph with a line with slope 1. I think this is a little too simple to be right, so what should I do here?
Thank you so much for your help.
Star Strider
Star Strider el 13 de Abr. de 2015
My pleasure. I’m glad it worked.
I did not run your code, but it would seem to me that a smaller step size would result in a smaller error. You are plotting log(err) against log(h), so I would expect a linear relationship. I believe your plot is likely correct.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Mathematics en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by