make nested for loop output a matrix and other error

Hello again turns out i asked the wrong question.
I am writing a code that compares the values of X to each in A, then running them through an equation. right now it outputs a matrix of columns [A, B, X, deflection] I want it to be a matrix of hight A (so row one is A(1) and so on) and length X (so column 1 is X(1)). therefor output(1,1) should be the deflection if A(1) and X(1) are plugged into an equation.
another problem I have is that when i change z to 1000 I get an error
"Array indices must be positive integers or logical values.
Error in bridge_start (line 55)
if a(i) > x(j)"
in the end i need A and X to each have 1000 point in them.
%get inputs first
E = 1.75*10^6 ; %lbf/in^2
I = 175 ; %in^4
L = 4;
p = 50;
equation1 = @(b,x) -((p.*b.*x)./(6*L*E*I)).*(L^2 -b.^2 - x.^2);
equation2 = @(a,b,x) -((p.*b.*x)./(6*L*E*I)).*(L^2 -b.^2 - x.^2)-((p.*(x-a).^3)./(6*E*I));
z = 4
[a,x]=deal(linspace(1,L,z))
b=L-a
%find data for subplot1
%one curve, max vs a
%outer loop
for i = linspace(1,L,z)
%inner loop
for j = linspace(1,L,z)
if a(i) > x(j)
deflection = equation1(b(i),x(j))
else
deflection = equation2(a(i),b(i),x(j))
end
row = sub2ind([4,4], a(j),x(i))
output(row, :) = [a(i),b(i),x(j), deflection ]
end
end
thank you all for the help. there seems to be a bit of a learning curve to Matlab

Respuestas (1)

KALYAN ACHARJYA
KALYAN ACHARJYA el 3 de Mayo de 2019
Editada: KALYAN ACHARJYA el 3 de Mayo de 2019
First One:
The question seems easy, but I did not undestand it exactly, sorry
Second one:
another problem I have is that when i change z to 1000 I get an error
"Array indices must be positive integers or logical values.
Say, as per your code-
>> L=4;
>> z=1000;
>> i=linspace(1,L,z)
i =
Columns 1 through 13
1.0000 1.0030 1.0060 1.0090 1.0120 1.0150 1......so on
Now if you want to acess a(i), its invalid, because of decimal, to access array indexing values must be interger and positive
When you consider z=4
then i having length of 1
>> i
i =
4
>> j
j =
4
>>
Please ensure the integer values of i and j, to ensure to avoid this error
>> i=linspace(1,z,L)
i =
1 334 667 1000
Or
>> i=1:L:z
i =
Columns 1 through 22
1 5 9 13 17 21 25 29 33 37 41 45 ...so on
Hope this helps for 2nd question.

1 comentario

thank you very much, also wi wash able to figure out the first question.
%get inputs first
E = 1.75*10^6 ; %lbf/in^2
I = 175 ; %in^4
L = 4;
p = 50;
equation1 = @(b,x) -((p.*b.*x)./(6*L*E*I)).*(L^2 -b.^2 - x.^2);
equation2 = @(a,b,x) -((p.*b.*x)./(6*L*E*I)).*(L^2 -b.^2 - x.^2)-((p.*(x-a).^3)./(6*E*I));
z = 4;
[a,x]=deal(linspace(1,L,z));
b=L-a;
results = zeros(z,z) ;
output = zeros(1,z);
%find data for subplot1
%one curve, max vs a
%outer loop
for i = 1:z
%inner loop
for j = 1:z
if a(i) >= x(j)
deflection = equation1(b(i),x(j))
else
deflection = equation2(a(i),b(i),x(j))
end
results(i,j) = deflection
deflection = results
end
end
this is what i ended up with and it seems to work alright. would you happen to know a more efficient way to rewrite this because it takes a hot min to calculate for 1000 points

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2018b

Preguntada:

el 3 de Mayo de 2019

Comentada:

el 3 de Mayo de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by