matlab图画不出来,Error using plot Vectors must be the same lengths.Error in Untitled (line 23) pH1=20;H2=20;H3=20;g=9.8h1 =[ ];h2=[ ];h3=[];for t =0:0.01:10if (sqrt(H1)-0.5*sqrt(2*g*t))> 0h1 =[h1,(sqrt(H1)-0.5*sqrt(2*g*t)).^2];elseh1 = [h1,0];en

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/18 02:48:00

matlab图画不出来,Error using plot Vectors must be the same lengths.Error in Untitled (line 23) pH1=20;H2=20;H3=20;g=9.8h1 =[ ];h2=[ ];h3=[];for t =0:0.01:10if (sqrt(H1)-0.5*sqrt(2*g*t))> 0h1 =[h1,(sqrt(H1)-0.5*sqrt(2*g*t)).^2];elseh1 = [h1,0];en
matlab图画不出来,Error using plot Vectors must be the same lengths.Error in Untitled (line 23) p
H1=20;H2=20;H3=20;g=9.8
h1 =[ ];h2=[ ];h3=[];
for t =0:0.01:10
if (sqrt(H1)-0.5*sqrt(2*g*t))> 0
h1 =[h1,(sqrt(H1)-0.5*sqrt(2*g*t)).^2];
else
h1 = [h1,0];
end
if (sqrt(H2+H1-h1)-0.5*sqrt(2*g*t))> 0
h2 = [h2,(sqrt(H2+H1-h1)-0.5*sqrt(2*g*t)).^2];
else
h2 =[h2,0];
end
if (sqrt(H3+H2-h2)-0.5*sqrt(2*g*t))> 0
h3 = [h3,(sqrt(H3+H2-h2)-0.5*sqrt(2*g*t)).^2];
else
h3 =[h3,0];
end
end
t=0:0.01:10;
subplot(2,2,1)
plot(t,h1);
subplot(2,2,2)
plot(t,h2);
subplot(2,2,3)
plot(t,h3);

matlab图画不出来,Error using plot Vectors must be the same lengths.Error in Untitled (line 23) pH1=20;H2=20;H3=20;g=9.8h1 =[ ];h2=[ ];h3=[];for t =0:0.01:10if (sqrt(H1)-0.5*sqrt(2*g*t))> 0h1 =[h1,(sqrt(H1)-0.5*sqrt(2*g*t)).^2];elseh1 = [h1,0];en
Error using plot Vectors must be the same lengths.
意思是t和h1,h2,h3的长度不是一致的.
到底问题出在哪里?
你的h1的值计算是没有错的,长度是一致的,
但是你的h2和h3的值所得出来的矩阵元素个数却大的惊人,
h2 = [h2,(sqrt(H2+H1-h1)-0.5*sqrt(2*g*t)).^2];
你看看,如果你的h1有n个元素,那么你的h2将最大有n(n+1)/2个元素,而你的h3中的元素将更大.
 
我粗略的理解了一下你的意思.
对于每一个t求出其当前的h1值,
将h1值代入sqrt(H2+H1-h1)-0.5*sqrt(2*g*t)).^2中来判断当前h2取0还是取sqrt(H2+H1-h1)-0.5*sqrt(2*g*t)).^2
再根据当前h2的值来确定当前h3的值.
 
那么很显然你的错误就是,没有把当前定义好.
 
下面给出代码:
H1=20;H2=20;H3=20;g=9.8;
h1 =[ ];h2=[ ];h3=[];
i=0;
for t =0:0.01:10
i=i+1;%%设置下标,来表示当前h1,h2,h3的元素
if (sqrt(H1)-0.5*sqrt(2*g*t))> 0 %%计算当前h1
h1(i)=(sqrt(H1)-0.5*sqrt(2*g*t)).^2;
else
h1(i) = 0;
end
if (sqrt(H2+H1-h1(i))-0.5*sqrt(2*g*t))> 0 %%计算当前h2
h2(i) = (sqrt(H2+H1-h1(i))-0.5*sqrt(2*g*t)).^2;
else
h2(i) =0;
end
if (sqrt(H3+H2-h2(i))-0.5*sqrt(2*g*t))> 0 %计算当前h3
h3(i) = (sqrt(H3+H2-h2(i))-0.5*sqrt(2*g*t)).^2;
else
h3(i)=0;
end
end
t=0:0.01:10;
subplot(2,2,1)
plot(t,h1);
subplot(2,2,2)
plot(t,h2);
subplot(2,2,3)
plot(t,h3);
 
你先看看,