Integral: Error in Numerical Integration, "Matrix dimensions must agree"

1 次查看(过去 30 天)
Apologies and thanks for your time at the start, I only started using matlab a month ago, and pretty much made this from scratch using the internet. Also I should really stop doing all of my Matlab stuff after midnight. I am trying to approximate the distance I'll throw with a trebuchet, and decide what lengths to use for certain parts. While doing this I encountered an error in the line where I try to integrate a formula. What I suspect is that it doesn't want to integrate because it might be getting it's input as a matrix, but that's where my insights end.
clear all; close all; clc
tic
savedmax = 0
m_weight = 0.224;
m_object = 0.008;
g = 9.81;
Theta_launch= 3/4*pi;
A = 0.0001435;
rho = 1100;
gamma = 88/13;
d = 0.01;
r_b = 0.044;
r_s = 0.0059;
epsilon = pi;
phi_e = 3/2*pi;
n_var_Theta=13;
Theta = zeros(n_var_Theta,1);
n_L_weight=11;
L_weight = zeros(n_L_weight,1);
n_L_throw=11;
L_throw = zeros(n_L_throw,1);
n_h_b=11;
h_b = zeros(n_h_b,1);
n_h_s=11;
h_s = zeros(n_h_s,1);
for k_var1 = [1:n_var_Theta]
Theta(k_var1,1) = 1/6.*pi.*k_var1 -1/6.*pi;
for k_var2 = [1:n_L_weight]
L_weight(k_var2,1) = 0.01713404.*k_var2 +0.205-0.017134041;
for k_var3 = [1:n_L_throw]
L_throw(k_var3,1) = 0.012.*k_var3 +0.155-0.012;
for k_var4 = [1:n_h_b]
h_b(k_var4,1) =0.04.*k_var4 -0.04;
for k_var5 = [1:n_h_s]
h_s(k_var5,1) =0.04.*k_var4 -0.04;
Lh_b = L_weight+h_b;
L_neg_hb = h_b-L_weight;
if Lh_b > 0.4
phi_b = -asin((0.4-h_b)./L_weight)+pi;
end
if Lh_b <= 0.4
phi_b=0.5.*pi;
end
if L_neg_hb < 0
phi_e = -asin(-h_b./L_weight)+pi;
end
if L_neg_hb >= 0
phi_e = 3/2.*pi;
end
Lorum = @(Theta)((-L_throw.*m_weight.*g.*cos(Theta)+ gamma.*L_weight.*m_object.*g.*cos(-gamma.*Theta+3.*pi./4-gamma.*(phi_e-phi_b)+gamma.*phi_e))./(1./4.*rho.*A.*(L_weight.^4)+1./2.*pi.*d.*rho.*(r_b.^2)+(L_weight.^2).*m_weight+gamma.*(1./4.*rho.*A.*(L_throw.^4)+1./2.*pi.*d.*rho.*(r_s.^2)+(L_throw.^2).*m_object)));
Ipsum = integral( Lorum , phi_b, phi_e);
omikron_b = sqrt(2.*abs(Ipsum));
omikron_s = gamma .* omikron_b;
v_0 = omikron_s.*L_throw;
h_s = h_b - sin(epsilon).*(r_s+r_b);
h_launch = h_s + L_throw .* sin(Theta_launch);
t= (-v_0.*sin(Theta_launch)-sqrt((v_0.*sin(Theta_launch)).^2+2.*g.*h_launch))./(-g);
s_x = v_0.*cos(Theta_launch).*t - sin(Theta_launch).*L_throw
maxcurrentcolumn = max(s_x) %output formulas
if maxcurrentcolumn >= savedmax
savedmax = maxcurrentcolumn
L_weightfinal = L_weight
L_throwfinal = L_throw
h_bfinal = h_b
h_sfinal = h_s
Theta_finals = Theta
end
end
end
end
end
end
toc
As my model is a bit of a mess I'll try to explain a bit of what you're seeing, and then focus on getting the integrating part to work. We're trying to do this for 5 variables, which are defined in the "for" -parts. Under the for parts there's 4 "if" parts which I don't think are working at the moment, which should define phi_e and phi_b. Running this resulted in an undefined phi_e, so it has a set value for now.
Beneath that is the part I'm having problems with.
Lorum = @(Theta)((-L_throw.*m_weight.*g.*cos(Theta)+ gamma.*L_weight.*m_object.*g.*cos(-gamma.*Theta+3.*pi./4-gamma.*(phi_e-phi_b)+gamma.*phi_e))./(1./4.*rho.*A.*(L_weight.^4)+1./2.*pi.*d.*rho.*(r_b.^2)+(L_weight.^2).*m_weight+gamma.*(1./4.*rho.*A.*(L_throw.^4)+1./2.*pi.*d.*rho.*(r_s.^2)+(L_throw.^2).*m_object)));
Ipsum = integral( Lorum , phi_b, phi_e);
Which gives the following error:
Error using .*
Matrix dimensions must agree.
Error in
Group_23_all_formulas_variablesneedtobeputin>@(Theta)(-painfully long formula-)
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in Group_23_all_formulas_variablesneedtobeputin (line 69)
Ipsum = integral( Lorum , phi_b, phi_e);
I'd appreciate any help I can get, as I'm feeling pretty lost at the moment. Thank you again for your time!

采纳的回答

Star Strider
Star Strider 2016-12-29
This solves the problem with ‘Ipsum’:
Ipsum = integral( Lorum , phi_b, phi_e, 'ArrayValued',true);
However, your code then goes into a series of loops that together execute a total of 17303 iterations. I stopped it early.
  5 个评论
Star Strider
Star Strider 2016-12-30
Thank you! I would appreciate the report!
Since it involves MATLAB, attaching it as a Comment to your Question here will be interesting to me personally and will help others. Please include your MATLAB code in it.
I have a M.S. in Biomedical Engineering, emphasising instrumentation, signal processing, and control theory (after my B.S., M.D., and Board Certification in Internal Medicine).
Our Biomedical Engineering projects never involved medieval siege warfare, but would have been much more interesting if they had!
Kevin van Dijk
Kevin van Dijk 2017-1-2
Will do! Due to some difficulties with the code in the form I presented earlier, I've decided to make a dumbed down version (decided to name it group_23_concrete) that actually works, and ask my group after the holidays if we have to try and get the one I sent earlier (group_23_parametric) working.
The requirements I set for the concrete version is to just calculate our projected distance thrown according to our formulas. It is working as intended at the moment, with some formulas that have to be adjusted. (we now calculate the inertia for solid gears, instead of the hollowed out ones we'll be using. I think we can also have it as a constant now, as the final design has been handed in. It also needs the air resistance formula put in.)
Playing around a bit with the variables that would be realistic gave me the following insights;
  • Shorter weight arm leads to a higher distance thrown
  • Longer throw arm leads to a higher distance thrown
Best I could do with what I think are realistic values for our design was 11.1258 meters, calculated in 0.06 sec.
My problems with the parametric model are at the moment:
  • It needs line 16 to define phi_e, otherwhise it's an undefined function, which means that my if statements for it aren't working. I also assume that I can't trust the defining of phi_b to happen properly because of this.
  • The part where it should save the specific variable used to obtain the highest s_x (our distance thrown) just saves the row of inputs for that variable (L_weightfinal ends up being the exact same thing as L_weight in the workspace.)
  • It spews out the same 11 numbers every iteration for s_x from 3.2 to 11.8686.(curiously close to 11.1258)
  • It takes 10-13 minutes for a full run.
I think the last two problems arise from me thinking Matlab would handle stuff in another way. I thought it would make matrices for Theta, L_weight etc, and then calculate s_x for the 1st number in every matrix. Then the second iteration it would do the 2nd number in the first matrix, and the 1st number for all other matrixes. Third iteration the 2nd number in the second matrix, and continue doing that to eventually have had an s_x for each and every combination.
This in my eyes should lead to a different output of numbers each iteration, which it isn't doing. But somehow it has found a combination that throws around the same distance as I predicted which is weird. As within the set parameters, I can get it to throw 16.1274 meters which isn't possible in reality because of ground constraints(just have to set L_throw to its highest, 0.275m). So it hasn't found the distance it was supposed to find either.
If you want to play around with variables in the concrete model, make sure to insert the right phi formulas, otherwise you'll end up with throwing 530 meters or something like that.
You have a respect commanding academic track record right there! Did you end up at a company in your M.S.'s field or in a hospital? And what kind of technology should I be imagining with your specialisation; (MRI-)scanners, eeg-scanners, or wearable monitoring devices? (for the last one, have a look at sensus.org, haven't looked into it yet myself, but I'm sure it's interesting)
Maybe if our trebuchet actually throws (I'm a bit nervous that the projectile might fall out with the multiple rotations and all that), I could challenge another group or something. Start a certain distance away from eachother, and both throw at the same time. After each throw, maybe each team can decide to go x cm in a direction of their choosing, first to score 3 hits wins?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by