Euler's method function problem.
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
Hi guys. We were tasked to solve a cauchy problem with the euler method, to then compare the error with the correct solution and graph them.
We came up with this function:
function [T]=eulero(x0,y0,fun,h,n_step)
%
% [T]= eulero(x0,y0,fun,h,n_step)
% Soluzione di un' equazione differenziale
% del primo ordine con il metodo di Eulero
%
% Dati di input:
% x0, y0: condizione iniziale
% fun: espressione di f(x,y)
% h: passo di discretizzazione
% n_step: numero dei passi da eseguire
%
% OUTPUT
% T = matrice di dimensione 2 x n_step
%     la prima riga contiene il vettore dei nodi,
%     la seconda riga contiene il vettore delle approssimazioni
%     della soluzione nei nodi
%
xi(1) = x0;
yi(1) = y0;
for i = 2:n_step+1
    yi(i) = yi(i-1) + h*fun(xi(i-1),yi(i-1));  % istruzione generale!!!
    xi(i) = x0 + (i-1) * h;
end
T = [xi;yi];
We then did this:
[T] = eulero (0,1,@(x,y)(y-2*sin(x)),0.25,8);
Followed by this:
DISEGNO LE SOLUZIONI (graficisoluzionitesina.m)
%%% DISEGNA LE FUNZIONI 
figure,
[T] = eulero (0,1,@(x,y)(y-2*sin(x)),0.25,8);
hold on,
plot (T(1,:), T(2,:), 'r'); grid on
xlabel ('x')
ylabel ('y')
y_true = @(x)(cos(x)+sin(x));
plot (T(1,:),y_true(T(1,:)),'c')
title ('Soluzioni')
legend ('0.25', 'vera', 'location', 'northwest')
Then this
DISEGNO L’ERRORE (disegnareerroretesina)
%%
%%%DISEGNA L'ERRORE
%%
EH = [];
for i = 1:1
    n_step = 8;
     [T] = eulero(0,1,@(x,y)(y-2*sin(x)),0.25,8);
     err = abs (T(2,:)-y_true(T(1,:)));
     EH = [EH max(err)];
     hold on, 
     plot (T(1,:),err, 'm');
     xlabel ('x')
     ylabel ('y')
end 
legend ('0.25', 'Location', 'Northwest')
figure, loglog (0.25,EH,'*')
xlabel ('0.25')
title ('Grafico dell''errore')
.
The problem we have is that we want to incorporate all of these codes (which if used separately work!) In a single code, to allow it to be used in a single function.
If we simply combine them, we get errors, and crashes.
Can you help us?
Thank you!
0 comentarios
Respuestas (2)
  Alan Stevens
      
      
 el 30 de Mayo de 2020
        How about the following (where I've left out most of your comment sections, but you can easily put them back):
%DISEGNO LE SOLUZIONI (graficisoluzionitesina.m)
%%% DISEGNA LE FUNZIONI 
[T] = eulero (0,1,@(x,y)(y-2*sin(x)),0.25,8);
y_true = @(x)(cos(x)+sin(x));
figure(1)
plot (T(1,:), T(2,:), 'r',T(1,:),y_true(T(1,:)),'c'); grid on
xlabel ('x')
ylabel ('y')
title ('Soluzioni')
legend ('Euler', 'True')
%DISEGNA L'ERRORE
err = abs (T(2,:)-y_true(T(1,:)));
EH = max(err);
ix = (err == EH);
EHx = T(1,ix);
figure(2)
plot (T(1,:),err, 'm',EHx,EH,'*'), grid
xlabel ('x')
ylabel ('y')
legend ('errore',['max error = ',num2str(EHx)])
title ('Grafico dell''errore')
function [T]=eulero(x0,y0,fun,h,n_step)
xi(1) = x0;
yi(1) = y0;
for i = 2:n_step+1
    yi(i) = yi(i-1) + h*fun(xi(i-1),yi(i-1));  % istruzione generale!!!
    xi(i) = x0 + (i-1) * h;
end
T = [xi;yi];
end
0 comentarios
Ver también
Categorías
				Más información sobre Calculus en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!