Calling function with different variables

1 view (last 30 days)
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!

Accepted Answer

KSSV
KSSV on 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

More Answers (1)

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

Sign in to comment.

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!