Filter löschen
Filter löschen

Calling function with different variables

1 Ansicht (letzte 30 Tage)
Alexander Engman
Alexander Engman am 9 Nov. 2016
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!

Akzeptierte Antwort

KSSV
KSSV am 9 Nov. 2016
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

Weitere Antworten (1)

Alexander Engman
Alexander Engman am 9 Nov. 2016
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 Kommentare
KSSV
KSSV am 9 Nov. 2016
It is called initializing the variables.
Alexander Engman
Alexander Engman am 9 Nov. 2016
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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Modify Image Colors finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by