For loop not working - How to properly loop inconsistent iterations with matrices?

2 visualizaciones (últimos 30 días)
Good day,
I am trying to loop and plot the results, but it is not working. The first iteration I made it manual because it is more of an 'initialization', and the code is not consistent as the 2,3rd, etc. iteration. When I run everything manually (no loop), it works, but when I start playing with the for loop, it gives me the following first error, which I cannot get rid of or do not know how:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in PartA_loop (line 48)
a(i+1) = (P(i+1)*area)./m;
Below is my full code, and it appears to fail after at the 2nd iteration iteration (i=2): Please note the commented if statement area, is failing for me as well. I am not too worried about that, I would like to get the code working, and if the "if" statement works as well it's a bonus!
I know it has to do with matrices, but I think I labeled everything correctly, so it should be pulling the correct numbers.
clear all
close all
clc
% constants
d=0.015; % value is in m, or 15mm
area = (1/4)*pi*(0.015)^2;
gamma = 1.66;
m = 0.002; %in kg
time_step = 0.00003; %intervals of 0.0003
% initial conditions
Po = 3.4474E+7; % Pa
c(1) = 1400; % m/s,
x(1) = 0.508; %in meters]
V0 = pi()*((d/2)^2).*x(1);
Pv_gamma0 = Po*V0^gamma;
t(1) = x(1)./c(1);
theta(1) = atand(t(1)./x(1));
a(1) = (Po*area)/m;
u0_0 = 0; %velocity before piston started to move is 0
u(1) = u0_0 + a(1)*time_step;
hyp(1) = -sqrt((t(1)^2)+(x(1)^2));
m(1) = -(t(1)./x(1));
p1 = [x(1),0];
p2 = [0,t(1)];
figure
plot([p1(1) p2(1)],[p1(2) p2(2)])
hold on
for i=1:5
disp(i) = u(i).*time_step;
x(i+1) = x(i) + disp(i);
V(i+1) = pi().*((d/2)^2).*(x(i+1));
P(i+1) = Pv_gamma0./(V(i)^gamma);
c(i+1) = u(i) - c(i);
% if c(i+1) < 0
% c(i+1) = abs(c(i+1));
% disp('CHECK OK: NEGATIVE VALUE')
% elseif c(i+1) > 0
% disp('ERROR CHECK: POSITIVE VALUE')
% end
t(i+1) = x(i+1)./c(i+1);
theta(i+1) = atand(t(i+1)./x(i+1));
a(i+1) = (P(i+1)*area)./m;
u(i+1) = u(i) + a(i+1).*time_step;
hyp(i+1) = -sqrt(((t(i+1))^2)+((x(i+1))^2));
m(i+1) = -(t(i+1))./(x(i+1));
p1 = [x(i),time_step.*i];
p2 = [0,t(i)];
plot([p1(1) p2(1)],[p1(2) p2(2)])
hold on
array1((i+1),1:2) = p1';
end
x
u
m
c
theta
hyp
I also attached the figure on how it should look like when I run the manual code instead.
Thank you in advance, I hope someone can assist!

Respuesta aceptada

Image Analyst
Image Analyst el 17 de Nov. de 2018
In the line
a(i+1) = (P(i+1)*area)./m;
m is an array after the first iteration. Do you mean
a(i+1) = (P(i+1)*area)./m(i);
  5 comentarios
Image Analyst
Image Analyst el 18 de Nov. de 2018
It works fine for me. See:
>> disp('CHECK OK: NEGATIVE VALUE')
CHECK OK: NEGATIVE VALUE
>>
I don't even see what index it's talking about on that line. There is no index at all. You don't have any variable called "disp" do you? What does this say?
which -all disp
cancel077
cancel077 el 18 de Nov. de 2018
Yeah I do have other variables called "disp", that was the issue. Just another simple mistake I did not see!
Thank you appreciate it! Just gotta figure it out now why it's failing on the 3rd iteration now.
Cheers!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by