converting float to integer makes problem

My code is:
% Uzunluk(cm)
e=2.25; a=15.75; b=13.5; g=44.25; L=90.50;
%Mesnet yükleri(kg)
C=3750; B=3750; A=6500; D=3750;
%Yayılı yükler(kg/cm)
q1=2*C*(L-e)/(L^2); q2=2*A*(L-a)/(L^2); q3=2*B*(L-a-b)/(L^2); q4=2*D*(L-a-b-g)/(L^2);
%Süperpozisyonla ayrılmış parçaların momentleri(kg.cm)%
%M1=-q1*(x^2)/2; M2=-q1*(x^2)/2+ C*(x-e);
%M3=-q2*(x^2)/2; M4=-q2*(x^2)/2+ A*(x-a);
%M5=-q3*(x^2)/2; M6=-q3*(x^2)/2+ B*(x-a-b);
%M7=-q4*(x^2)/2; M8=-q4*(x^2)/2+ D*(x-e);
% Süperpozisyonla ayrılan parçaların birleştirilmesi sonucu net
% momentler(kg.com)
for x=0:0.025:L
y=int64(40*x)+1;
if (0<=x) && (x<=e)
y
M(y)=-(x^2)/2*(q1+q2+q3+q4);
elseif (e<x) && (x<=a)
y
M(y)=-(x^2)/2*(q1+q2+q3+q4)+C(x-e);
elseif (a<x) && (x<=a+b)
M(y)=-(x^2)/2*(q1+q2+q3+q4)+C(x-e)+A(x-a);
elseif (a+b<x) && (x<=a+b+g)
M(y)=-(x^2)/2*(q1+q2+q3+q4)+C(x-e)+A(x-a)+B(x-a-b);
elseif (a+b+g<x) && (x<=L)
M(y)=-(x^2)/2*(q1+q2+q3+q4)+C(x-e)+A(x-a)+B(x-a-b)+D(x-a-b-g);
end
end
Runner finished first if loop and after that even if it calculates the value of y as 92 it gave me that error:
Array indices must be positive integers or logical values.
Error in untitled2 (line 23)
M(y)=-(x^2)/2*(q1+q2+q3+q4)+C(x-e);
error.
How can i fix that?

 Accepted Answer

int64(y);
That line takes a value of y as input, and creates a uint64 equivalent of the input value, and stores the value in the semi-hidden variable named ans and then otherwise throws away the result because the semi-colon tells MATLAB not to display the output.
One thing it does not do is store the result into y . If you want the result to be written into y then you need to store the result into y

4 Comments

for x=0:0.1:L
y=10*x+1;
int64(y);
y
I recommend you replace that with
for y = 1 : 10*L + 1
x = (y-1)/10;
The logic is more simple and has less opportunity for round-off error. It also avoids the problem that when you use one of the internet data type conversions such as uint8() or int64() that the values are rounded instead of truncated.
My code was like that:
% Uzunluk(cm)
e=2.25; a=15.75; b=13.5; g=44.25; L=90.50;
%Mesnet yükleri(kg)
C=3750; B=3750; A=6500; D=3750;
%Yayılı yükler(kg/cm)
q1=2*C*(L-e)/(L^2); q2=2*A*(L-a)/(L^2); q3=2*B*(L-a-b)/(L^2); q4=2*D*(L-a-b-g)/(L^2);
%Süperpozisyonla ayrılmış parçaların momentleri(kg.cm)%
%M1=-q1*(x^2)/2; M2=-q1*(x^2)/2+ C*(x-e);
%M3=-q2*(x^2)/2; M4=-q2*(x^2)/2+ A*(x-a);
%M5=-q3*(x^2)/2; M6=-q3*(x^2)/2+ B*(x-a-b);
%M7=-q4*(x^2)/2; M8=-q4*(x^2)/2+ D*(x-e);
% Süperpozisyonla ayrılan parçaların birleştirilmesi sonucu net
% momentler(kg.com)
for y = 1 : 40*L + 1
x = (y-1)/40;
%y=int64(40*x)+1;
%int64(y);
%y
if (0<=x) && (x<=e)
y
M(y)=-(x^2)/2*(q1+q2+q3+q4);
elseif (e<x) && (x<=a)
y
M(y)=-(x^2)/2*(q1+q2+q3+q4)+C(x-e);
elseif (a<x) && (x<=a+b)
M(y)=-(x^2)/2*(q1+q2+q3+q4)+C(x-e)+A(x-a);
elseif (a+b<x) && (x<=a+b+g)
M(y)=-(x^2)/2*(q1+q2+q3+q4)+C(x-e)+A(x-a)+B(x-a-b);
elseif (a+b+g<x) && (x<=L)
M(y)=-(x^2)/2*(q1+q2+q3+q4)+C(x-e)+A(x-a)+B(x-a-b)+D(x-a-b-g);
end
end
y = 1
y = 2
y = 3
y = 4
y = 5
y = 6
y = 7
y = 8
y = 9
y = 10
y = 11
y = 12
y = 13
y = 14
y = 15
y = 16
y = 17
y = 18
y = 19
y = 20
y = 21
y = 22
y = 23
y = 24
y = 25
y = 26
y = 27
y = 28
y = 29
y = 30
y = 31
y = 32
y = 33
y = 34
y = 35
y = 36
y = 37
y = 38
y = 39
y = 40
y = 41
y = 42
y = 43
y = 44
y = 45
y = 46
y = 47
y = 48
y = 49
y = 50
y = 51
y = 52
y = 53
y = 54
y = 55
y = 56
y = 57
y = 58
y = 59
y = 60
y = 61
y = 62
y = 63
y = 64
y = 65
y = 66
y = 67
y = 68
y = 69
y = 70
y = 71
y = 72
y = 73
y = 74
y = 75
y = 76
y = 77
y = 78
y = 79
y = 80
y = 81
y = 82
y = 83
y = 84
y = 85
y = 86
y = 87
y = 88
y = 89
y = 90
y = 91
y = 92
Array indices must be positive integers or logical values.
%%Momentlerin grafik üzerinde çizdirilmesi
%plot(x, M, 'r-', 'LineWidth', 2);
%title('Bending Moment Diagram');
%xlabel('Position (m)');
%ylabel('Bending Moment (N.m)');
%grid on;
It still gives me error like:
y =
92
Array indices must be positive integers or logical values.
Error in untitled (line 24)
M(y)=-(x^2)/2*(q1+q2+q3+q4)+C(x-e);
92 is integer however it didn't work. I couldn't get it.
x = (y-1)/40;
That is not going to be an integer.
M(y)=-(x^2)/2*(q1+q2+q3+q4)+C(x-e);
but x-e is used as a subscript
C=3750; B=3750; A=6500; D=3750;
... a subscript into the scalar value C
Reminder: MATLAB has absolutely no implied multiplication. Not anywhere . Not even inside the internal language of the Symbolic Mathematics toolbox.
That was my fault that rather than multiplying with C, i indicated C as array.
Thanks for help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!