Calling function with different variables

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Modify Image Colors 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by