Extract a function from a table

Hello,
I really need to clear up my toughts, I want to extract a fonction from a table of two dimensions ( l,t) that I recover from and ODE method.
[t,l]=ode23('odef',[t0,tf],l0)
My goal is to integrate "l" Si I need to have a function to use for exemple the simpson method :
f = inline('l','t')
h = 100/N;
Isim=0.0;
for i=1:N
Isim= Isim+h*(1/6*f(t(i))+2/3*f((t(i)+t(i+1))/2)+1/6*f(t(i+1)));
end
Isim
Of corse this program doesn't work because I used a vecto as a function !
Do you have an idea how can I integrate from data ?
Thank you,
Regards,

14 Comments

Do a substitution of functions l = dL and solve for L.
In other words just add one more state variable that is fed from the current variable, and the output for that new variable will automatically be the numeric integration.
Thank you for your response, I don't really understand what do you mean by dL, is d the derivation ? How can I do this on matlab, because if I put l=dL it doesn't solve my problem...
For example if you had
function dx = odef(t, x)
dx(1,1) = 2*x-sin(x(1));
Then you would change to
function dx = odef(t, x)
dx(1,1) = 2*x-sin(x(1));
dx(2,1) = x(1);
And now the second column of output would be the numeric integration of the first column.
Hello,
thank you very much for your answer, I tryed it and I have the second column as the integral !
But I want to know if it is possible to do a specific method such as Simpson or by Usine Splines ?
I used interpolation to generate a function : But I have an error
Dl=[t,l];
x=Dl(:,1);
y=Dl(:,2);
plot(x,y,'*')
format long;
%Splines cubic
ys=spline(x,y)
ysplot=ppval(ys,t);
plot(x,y,'b*',t,ysplot,'r+',x,y2)
int=diff(fnval(fnint(ys),[0 4]))
The error is on the final line (int) : Array indices must be positive integers or logical values.
ys = struct with fields:
form: 'pp'
breaks: [1×96 double]
coefs: [95×4 double]
pieces: 95
order: 4
dim: 1
So I don't know if I can fix it ?
Thank you,
regards,
Is it possible that you have a variable named diff?
Where ? Sorry, I don't understand you are talking about my program ? I didn't use diff.. diff is derivation in matlab..
You indicate that on the line
int=diff(fnval(fnint(ys),[0 4]))
you are getting an error about array indices. You should check
which fnint
which fnval
which diff
to see if any of them are variables instead of functions at that point in your code.
Hello sir,
Thank you for your answer
I did :
which fnint
which fnval
which diff
And I had this :
which fnval
C:\Program Files\MATLAB\R2019b\toolbox\curvefit\splines\fnval.m
>> which fnint
C:\Program Files\MATLAB\R2019b\toolbox\curvefit\splines\fnint.m
>> which diff
built-in (C:\Program Files\MATLAB\R2019b\toolbox\matlab\datafun\@char\diff) % char method
Try this series of steps and see which one reports the subscript error:
temp1 = fnint(ys);
temp2 = [0 4];
temp3 = fnval(temp1, temp2);
int = diff(temp3);
Thank you,
I found that the probleme is ;
int = diff(temp3);
That means I have a probleme of array ..
when I try :
int=fnval(fnint(ys),[0 4])
it gives me :
int = 1×2
0 1.357601954491484
And the diff.m is :
%DIFF Difference and approximate derivative.
% DIFF(X), for a vector X, is [X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)].
% DIFF(X), for a matrix X, is the matrix of row differences,
% [X(2:n,:) - X(1:n-1,:)].
% DIFF(X), for an N-D array X, is the difference along the first
% non-singleton dimension of X.
% DIFF(X,N) is the N-th order difference along the first non-singleton
% dimension (denote it by DIM). If N >= size(X,DIM), DIFF takes
% successive differences along the next non-singleton dimension.
% DIFF(X,N,DIM) is the Nth difference function along dimension DIM.
% If N >= size(X,DIM), DIFF returns an empty array.
%
% Examples:
% h = .001; x = 0:h:pi;
% diff(sin(x.^2))/h is an approximation to 2*cos(x.^2).*x
% diff((1:10).^2) is 3:2:19
%
% If X = [3 7 5
% 0 9 2]
% then diff(X,1,1) is [-3 2 -3], diff(X,1,2) is [4 -2
% 9 -7],
% diff(X,2,2) is the 2nd order difference along the dimension 2, and
% diff(X,3,2) is the empty matrix.
%
% See also GRADIENT, SUM, PROD.
% Copyright 1984-2005 The MathWorks, Inc.
% Built-in function.
Thank you very much sir, I solved the problem of Diff.
Now I have to resolve how can I have the integration vectof of l=y. ^^
Thank you Walter Roberson.
Regards,
What did the problem turn out to be?
I made a mistake, I added another variable Like you said before on the top of the code diff=abs(l1(1:80)-l(1:80)); and I forgot about it.. Thank you

Sign in to comment.

Answers (0)

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Asked:

on 17 Nov 2019

Commented:

on 20 Nov 2019

Community Treasure Hunt

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

Start Hunting!