Error when computing integral
이전 댓글 표시
I want to find the integral value for the following, and keep on getting an assort of errors.
syms Xs
for i=1:1:N
phi_i=Xs^(i-1)*(1-cos(Xs*pi / (2*L)));
d2_phi(i,:)= diff(phi_i,Xs,2);
end
i = 1;
j=1;
fun = @(Xs) EI*d2_phi(i)*(d2_phi(j))
q = integral(fun,Xs,0,L)
These values are supposed to be the ij values in an NxN matrix, and EI is just a numeric variable.
답변 (1개)
Star Strider
2018년 1월 13일
This runs without error:
syms Xs i L
phi_i(Xs,i,L) = Xs^(i-1)*(1-cos(Xs*pi / (2*L)));
d2_phi(Xs,i,L) = diff(phi_i,Xs,2);
D2_Phi = matlabFunction(d2_phi); % Do Symbolic Operations, Then Convert To Anonymous Function
i=1:3;
j=1:3;
EI = 42; % Provide Correct Value
L = 4.2; % Provide Correct Value
fun = @(Xs) EI*D2_Phi(Xs,i,L).*D2_Phi(Xs,j,L);
q = integral(fun,0,L, 'ArrayValued',true)
You may need to change it to work the way you want with your data.
댓글 수: 2
Lucas Cohen
2018년 1월 13일
Star Strider
2018년 1월 13일
You have to put the symbolic operations first, so the parameter assignments are numeric rather than symbolic. You can also completely vectorise the integral call:
% %%Finding the derivatives:
syms Xs i L
phi_i(Xs,i,L) = Xs^(i-1)*(1-cos(Xs*pi / (2*L)));
d2_phi(Xs,i,L) = diff(phi_i,Xs,2);
D2_Phi = matlabFunction(d2_phi);
E = 200e9 ; %[Pa]
I = 8.6e-4; %[m^4]
L = 10; %[m]
p = 200; %[N/m]
EI = E*I; %[N*m^2]
dx = 0.01;
x = [0:dx:L];
N = 5; %Degree of Rayleigh-Ritz function
fun = @(Xs,i,j,L) EI*D2_Phi(Xs,i,L).*D2_Phi(Xs,j,L);
% %%K Matrix
K = zeros(N,N);
[I,J] = meshgrid(1:N);
K = integral(@(Xs)fun(Xs,I,J,L),0,L, 'ArrayValued',true);
With the meshgrid call it may not be necessary to preallocate ‘K’, although that would probably not slow your code.
카테고리
도움말 센터 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!