Obtaining integral function from code
    5 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    john_arle
 el 19 de Jun. de 2019
  
    
    
    
    
    Respondida: Star Strider
      
      
 el 19 de Jun. de 2019
            Hello there!
I am trying to obtain the integral function of a complicated relation. 
Here though, to check out the code, I am implementing the integral of f(x) = x. 
However, I get an error when the index i of my function reaches 0.7. 
Could please tell me why?
Thanks, 
Have a good day. 
Andrea. 
MAIN:
x = [0:0.1:1];
prova = @(x) x;
figure
res(1:end) = fun_integra(prova, 0, 1, 0.1);
plot(x,prova(x))
figure 
plot(x, res(1:end))
FUNCTION fun_integra
function [res] = fun_integra(integrand, low, up, step)
    for i=low:step:(up-step)
        res((i-low)/(step)+1) = integral(integrand, low, i+step);
    end
end
0 comentarios
Respuesta aceptada
  Star Strider
      
      
 el 19 de Jun. de 2019
        At that value, the subscript is no longer an integer.  This is most likely a problem with floating-point calculation error.  Using the fix (link) function to force the subscript to be an integer corrects it: 
res(fix((i-low)/(step)+1)) = integral(integrand, low, i+step);
That leaves the problem of ‘x’ and ‘res’ not being the same lengths in:
res = fun_integra(prova, 0, 1, 0.1);
 fixed by : 
plot(x(1:numel(res)), res(1:end))
0 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Matrix Indexing 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!

