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 个)

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 个评论

Here is the corrected version:
%%Parameters
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
%%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);
%%K Matrix
K = zeros(N,N);
i=1:3;
j=1:3;
fun = @(Xs) EI*D2_Phi(Xs,i,L).*D2_Phi(Xs,j,L);
K(i,j) = integral(fun,0,L, 'ArrayValued',true);
K
And these are the errors I keep getting:
Error using integral (line 85)
A and B must be floating-point scalars.
Error in HW10 (line 28)
K(i,j) = integral(fun,0,L, 'ArrayValued',true);
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.

请先登录,再进行评论。

类别

Community Treasure Hunt

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

Start Hunting!

Translated by