필터 지우기
필터 지우기

Calling function with different variables

조회 수: 1 (최근 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 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