Calling function with different variables

4 ビュー (過去 30 日間)
Alexander Engman
Alexander Engman 2016 年 11 月 9 日
コメント済み: Alexander Engman 2016 年 11 月 9 日
Hi!
I have a function which looks like this:
function dZ=undervatten(n,x,Z)
p=[5.8795547370783 14.4873921534832 257.1922990416989];
c=4800+p(1)+((p(2))*2)+((p(3))*exp(-2));
q0=(c/cosd(n));
% Z(1):=avstånd
% Z(2):=vinkel
dZ=zeros(2,1);
dZ(1)=Z(2);
dZ(2)=-q0^2*(((-p(3)/1000)*exp(-Z(1)/1000))+p(2)/1000)/((4800+p(1)+(p(2)*Z(1)/1000)+(p(3)*exp(-Z(1)/1000))^3));
end
Then I want to be able to call this function for different values of n (which is in q0).
iter=1
for n=-10:14
[X,Z]=ode45(@undervatten,[0:3000],[2000 tand(n)]);
[value(iter)=Z(end,1)
iter=iter+1
end;
n represents starting angles for a sound wave, so basically I want to be able to call the function with angles from -10:14 degrees and then save the end value of Z in a vector, to see what values (depth) the different starting angles give after a certain distance (x). How can I change my code to be able to call different values of n for each iteration?
Thank you!

採用された回答

KSSV
KSSV 2016 年 11 月 9 日
clc; clear all ;
n = -10:14 ;
X = zeros(length(n),90) ;
Z = zeros(length(n),90) ;
for i = 1:length(n)
[x,z]=ode45(@undervatten,[0:3000],[2000 tand(n(i))]);
X(i,:) = x ;
Z(i,:) = z(:,1) ;
end;
function dZ=undervatten(n,Z)
p=[5.8795547370783 14.4873921534832 257.1922990416989];
c=4800+p(1)+((p(2))*2)+((p(3))*exp(-2));
q0=(c/cosd(n));
% Z(1):=avstånd
% Z(2):=vinkel
dZ=zeros(2,1);
dZ(1)=Z(2);
dZ(2)=-q0^2*(((-p(3)/1000)*exp(-Z(1)/1000))+p(2)/1000)/((4800+p(1)+(p(2)*Z(1)/1000)+(p(3)*exp(-Z(1)/1000))^3));
end

その他の回答 (1 件)

Alexander Engman
Alexander Engman 2016 年 11 月 9 日
Thank you! I understand what I did wrong now.
Just one question, from where does the number 90 come from, in:
X = zeros(length(n),90) ;
Z = zeros(length(n),90) ;
?
  2 件のコメント
KSSV
KSSV 2016 年 11 月 9 日
It is called initializing the variables.
Alexander Engman
Alexander Engman 2016 年 11 月 9 日
Yes I understand that you have to initialize it but what I am wondering is, how did you know that this number should be 90? What command is creating 90 columns in x and z?

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by