Having errors in a multivariable for loop.
Show older comments
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!
Accepted Answer
More Answers (1)
Arif Hoq
on 30 Mar 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
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!