Scatter plot: X and Y must be vectors of the same length

5 ビュー (過去 30 日間)
Dereje
Dereje 2018 年 4 月 4 日
コメント済み: Dereje 2018 年 4 月 4 日
I want to plot the maximum height of an array. I am getting an error : X and Y must be vectors of the same length. Thanks for the help.
%
zspan=[0,400];
v0mat = [1 0.01 1;1 0.05 1;1 0.1 1;1 0.2 1];
zsol = {};
v1sol = {};
v2sol = {};
v3sol = {};
for k=1:size(v0mat,1)
v0=v0mat(k,:);
[z,v]=ode45(@rhs,zspan,v0);
[~, Nsqr]=density1(z);
zsol{k}=z;
v1sol{k}=v(:,1);
v2sol{k}=v(:,2);
v3sol{k}=v(:,3);
%Maximum height
ZH{k}=3.76*(pi.*v1sol{k}.*v1sol{k}.*v2sol{k}.*v3sol{k}).^(1/4).*(Nsqr.^(-3/4));
end
for r=1:length(ZH)
q(r)=r;
end
for k2 = 1:length(ZH)
Mzsol(k2)= max((ZH{k2}));
end
figure()
scatter(q,max(Mzsol),'g')
function [rho, Nsqr]=density1(z)
%%Data for desnity with respect to depth
zval= [2 3 5 7 10 15 20 25 30 40 50 60 70 80 90 100 125 150 160 175 200 225 250 275 300 325 350 375 400];
rho1 = [17.2731684 17.1649375 21.43455647 22.4140625 23.86332207 24.3746967 24.70487685 24.6003125 24.8933125 25.42772826 26.03220776 26.439625 26.8151875 26.86830797 27.1949375 27.34406944 27.5551875 27.728625 27.23423729 27.88542857 27.752249049 28.1025 28.2415 28.37 28.05366667 28.6565 28.7755 28.898 29.013];
rho0=29;g=9.8;
zvala=400-zval;
zvalue=fliplr(zvala);
rho2=fliplr(rho1);
rho3=smoothdata(rho2,'lowess',5);
rho=interp1(zvalue,rho3,z);
rho4=interp1(zvalue,rho3,z+0.1);
N=(-g./rho0).*(rho4-rho);
Nsqr= sqrt(N);
end
function parameters=rhs(z,v)
[~, Nsqr]=density1(z);
alpha=0.116;
db= 2*alpha-(v(1).*v(3))./(2*v(2).^2);
dw= (v(3)./v(2))-(2*alpha*v(2)./v(1));
dgmark= -Nsqr.*Nsqr-(2*alpha*v(3)./v(1));
parameters=[db;dw;dgmark];
end
  2 件のコメント
Adam
Adam 2018 年 4 月 4 日
The error is fairly self-explanatory if you follow where it points to and understand it is the two inputs to the scatter function that must be the same size, i.e.
q and max(Mzsol) must be the same size as each other. Mzsol appears to be a vector in your code so the max of it is just a scalar whereas q appears to also be a vector so how would you expect to scatter plot a vector against a scalar?
Dereje
Dereje 2018 年 4 月 4 日
Yes you are right. But I have four values of max(Mzsol) (for each array ) which would be 1x4 and q is also 1x4. Here is that I want to make a connection between the two for the scatter plot when q=1, I want to take the first value in Mzsol, ...

サインインしてコメントする。

採用された回答

Aletta Wilbrink
Aletta Wilbrink 2018 年 4 月 4 日
The problem is in the code
scatter(q,max(Mzsol),'g')
where q is yoour x and max(Mzsol) is your y.
max(MZsol) gives you 1 number, where q is a 1x4 double.
I don't know what you exactly want to show in this figure, but these two variables has to be the same size, the next code should be able to work
scatter(q,Mzsol,'g')
But again, I don't know if this is what you want
  1 件のコメント
Dereje
Dereje 2018 年 4 月 4 日
I see my problem, I shouldn't include max in the plot. Thanks a lot!! This is exactly what I wanted. y is max height and x is cases where for different conditions.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by