0
votes

having a problem with my "new love", matlab: I wrote a function to calculate an integral using the trapz-method: `

function [L]=bogenlaenge_innen(schwingungen)
R = 1500;            %Ablegeradius
OA = 1;              %Amplitude
S = schwingungen;    %Schwingungszahl
B = 3.175;           %Tapebreite

phi = 0:2.*pi./10000:2.*pi;

BL = sqrt((R-B).^2+2.*(R-B).*OA.*sin(S.*phi)+OA.^2.*(sin(S.*phi)).^2+OA.^2.*S.^2.*(cos(S.*phi)).^2);   

L = trapz(phi,BL)`

this works fine when i start it with one specific number out of the command-window. Now I want to plot the values of "L" for several S.

I did the following in a new *.m-file: W = (0:1:1500); T = bogenlaenge_innen(W); plot(W,T)

And there it is:

Error using .* Matrix dimensions must agree.

What is wrong? Is it just a dot somewhere? I am using matlab for the second day now, so please be patient.... ;) Thank you so much in advance!

PS: just ignore the german part of the code, it does not really matter :)

3
You should have a look at the size of T - fatihk

3 Answers

0
votes

In your code, the arrays S and phi in the expression sin(S.*phi) should have same size or one of them should be a constant in order the code works

0
votes

The error is most likely because you have made it so that the number of elements in schwingungen, i.e. W in your code, must be equal to the number of elements in phi. Since size(W) gives you a different result from size(0:2.*pi./10000:2.*pi), you get the error.

The way .* works is that is multiplies each corresponding elements of two matrices provided that they either have the same dimensions or that one of them is a scalar. So your code will work when schwingungen is a scalar, but not when it's a vector as chances are it has a different number of elements from the way you hard coded phi.

The simplest course of action (not necessarily the most Matlabesque though) for you is to loop through the different values of S:

W = (0:1:1500);
T = zeros(size(W); %Preallocate for speed)

for ii = 1:length(W)
    T(ii) = bogenlaenge_innen(W(ii));
end

plot(W,T)
0
votes

In your function you define phi as a vector of 10001 elements. In this same function you do S.*phi, so if S is not the same length as phi, you will get the "dimensions must agree" error. In your call to the function you are doing it with a vector of length 1501, so there is your error.

Regards