Having errors in a multivariable for loop.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Gabriel Quattromani
am 29 Mär. 2022
Bearbeitet: VBBV
am 30 Mär. 2022
I have this following code in order to find the values of vmax with varying h and x. The values are to be inputted into a single matrix. I am receiving the error message: Array indices must be positive integers or logical values.
function [vmax] = v1(temp_diff,h)
vmax=0.083*sqrt(temp_diff*h);
end
function [vmax] = v2(x,temp_diff,h)
vmax=[0.143/(x+1.32)]*sqrt(temp_diff*h);
end
function [vmax] = v3(temp_diff,h)
vmax=0.043*sqrt(temp_diff*h);
end
temp_int=24;
temp_ext=10;
U=5;
hi=4;
h=1:5;
temp_diff=(U/hi)*(temp_int-temp_ext);
x=0:0.1:2.5;
X=repmat(x,size(h,2),1);
H=repmat(x,size(x,2),2);
n=length(x);
m=length(h);
V=zeros(n,m);
for i=0:0.1:2.5
for h=1:5
if i<0.4
vmax(i)=v1(temp_diff(i),h(i));
elseif i<=2
vmax(i)=v2(temp_diff(i),h(i),x(i));
else
vmax(i)=v3(temp_diff(i),h(i));
end
end
end
Hoping someone with more experience can help me out!
0 Kommentare
Akzeptierte Antwort
VBBV
am 30 Mär. 2022
Bearbeitet: VBBV
am 30 Mär. 2022
temp_int=24;
temp_ext=10;
U=5;
hi=4;
h=1:5;
temp_diff=(U/hi)*(temp_int-temp_ext);
I = 0:0.1:2.5;
x = 0:0.1:2.5;
n=length(x);
m=length(h);
V=zeros(n,m);
X=repmat(x,size(h,2),1);
H=repmat(x,size(x,2),2);
for ii = 1:length(I) % chnage the loop index // variable //from 0 to 1
for h=1:5
if I(ii)<0.4
vmax(ii)=v1(temp_diff,h);
elseif I(ii)<=2
vmax(ii)=v2(temp_diff,h,x(ii));
else
vmax(ii)=v3(temp_diff,h);
end
end
end
plot(I,vmax)
function [vmax] = v1(temp_diff,h)
vmax=0.083*sqrt(temp_diff*h);
end
function [vmax] = v2(x,temp_diff,h)
vmax=[0.143/(x+1.32)]*sqrt(temp_diff*h);
end
function [vmax] = v3(temp_diff,h)
vmax=0.043*sqrt(temp_diff*h);
end
Modfy the loop so that array indices are positive integers and not zeros
0 Kommentare
Weitere Antworten (1)
Arif Hoq
am 30 Mär. 2022
try this:
temp_int=24;
temp_ext=10;
U=5;
hi=4;
h=1:5;
temp_diff=(U/hi)*(temp_int-temp_ext);
x=0:0.1:2.5;
X=repmat(x,size(h,2),1); % you are not using this parameter in your code
H=repmat(x,size(x,2),2); % you are not using this parameter in your code
n=length(x);
m=length(h);
V=zeros(n,m);
for i=1:5 % for loop for value of h
for j=1:length(x) % for loop for value of x
if i<0.4
vmax(i)=v1(temp_diff,h(i)); % as temp_diff is a scalar so you don't need indexing
elseif i<=2
vmax(i)=v2(temp_diff,h(i),x(j));
else
vmax(i)=v3(temp_diff,h(i));
end
end
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!