You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Extract a function from a table
1 view (last 30 days)
Show older comments
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
Walter Roberson
on 17 Nov 2019
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.
Sarah CHOUCHENE
on 17 Nov 2019
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...
Walter Roberson
on 17 Nov 2019
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.
Sarah CHOUCHENE
on 17 Nov 2019
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,
Walter Roberson
on 17 Nov 2019
Is it possible that you have a variable named diff?
Sarah CHOUCHENE
on 18 Nov 2019
Where ? Sorry, I don't understand you are talking about my program ? I didn't use diff.. diff is derivation in matlab..
Walter Roberson
on 18 Nov 2019
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.
Sarah CHOUCHENE
on 20 Nov 2019
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
Walter Roberson
on 20 Nov 2019
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);
Sarah CHOUCHENE
on 20 Nov 2019
Edited: Sarah CHOUCHENE
on 20 Nov 2019
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
Sarah CHOUCHENE
on 20 Nov 2019
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.
Sarah CHOUCHENE
on 20 Nov 2019
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,
Walter Roberson
on 20 Nov 2019
What did the problem turn out to be?
Sarah CHOUCHENE
on 20 Nov 2019
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
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)