Extract a function from a table
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2019-11-17
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
2019-11-17
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
2019-11-17
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
2019-11-17
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,
Sarah CHOUCHENE
2019-11-18
Where ? Sorry, I don't understand you are talking about my program ? I didn't use diff.. diff is derivation in matlab..
Walter Roberson
2019-11-18
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
2019-11-20
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
2019-11-20
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
2019-11-20
编辑:Sarah CHOUCHENE
2019-11-20
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
2019-11-20
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
2019-11-20
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,
Sarah CHOUCHENE
2019-11-20
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
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Mathematics and Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
