Having errors in a multivariable for loop.

조회 수: 1 (최근 30일)
Gabriel Quattromani
Gabriel Quattromani 2022년 3월 29일
편집: VBBV 2022년 3월 30일
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!

채택된 답변

VBBV
VBBV 2022년 3월 30일
편집: VBBV 2022년 3월 30일
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

추가 답변 (1개)

Arif Hoq
Arif Hoq 2022년 3월 30일
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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by