MATLAB: Save outputs of a function to a vector for different inputs

different inputs for a functionsave as a vectorsave outputs

Dear all I have a function with some constant outputs and one variable input. For example, for 4 different angles(i1) I need to save the output Rpp for each angle and place it in a vector with a size of (1,4). Please help me with this question. I got stuck and cannot go further. Thanks in advance I have the following codes;
function [ Rpp ] = sszoeppritz( Vp1,Vs1,rho1,Vp2,Vs2,rho2,i1 )
Vp1=3300;
Vp2=3850;
rho1=2390;
rho2=2510;
Vs1=1500;
Vs2=2000;
i1=[10 15 35 40]
p=sin(i1)/Vp1;
i2=asin(p*Vp2);
j1=asin(p*Vs1);
j2=asin(p*Vs2);
a=rho2*(1-2*Vs2^2*p.^2)-rho1*(1-2*Vs1^2*p^2);
b=rho2*(1-2*Vs2^2*p.^2)+2*rho1*Vs1^2*p^2;
c=rho1*(1-2*Vs1^2*p.^2)+2*rho2*Vs2^2*p^2;
d=2*(rho2*Vs2^2-rho1*Vs1^2);
E=b*cos(i1)/Vp1+c*cos(i2)/Vp2;
F=b*cos(j1)/Vs1+c*cos(j2)/Vs2;
G=a-d*(cos(i1)/Vp1)*(cos(j2)/Vs2);
H=a-d*(cos(i2)/Vp2)*(cos(j1)/Vs1);
D=E*F+G*H*p^2;
Rpp=((b*(cos(i1)/Vp1)-c*cos((i2)/Vp2))*F-(a+d*((cos(i1)/Vp1))*(cos(j2)/Vs2))*H*p^2)/D;
end

Best Answer

You need to vectorise everything and your code runs:
Vp1=3300;
Vp2=3850;
rho1=2390;
rho2=2510;
Vs1=1500;
Vs2=2000;
i1=[10 15 35 40];
p=sin(i1)/Vp1;
i2=asin(p*Vp2);
j1=asin(p*Vs1);
j2=asin(p*Vs2);
a=rho2*(1-2*Vs2^2*p.^2)-rho1*(1-2*Vs1.^2*p.^2);
b=rho2*(1-2*Vs2^2*p.^2)+2*rho1*Vs1^2*p.^2;
c=rho1*(1-2*Vs1^2*p.^2)+2*rho2*Vs2^2*p.^2;
d=2*(rho2*Vs2^2-rho1*Vs1^2);
E=b.*cos(i1)./Vp1+c.*cos(i2)/Vp2;
F=b.*cos(j1)./Vs1+c.*cos(j2)/Vs2;
G=a-d*(cos(i1)/Vp1).*(cos(j2)/Vs2);
H=a-d*(cos(i2)/Vp2).*(cos(j1)/Vs1);
D=E.*F+G.*H.*p.^2;
Rpp=((b.*(cos(i1)/Vp1)-c.*cos((i2)/Vp2)).*F-(a+d*((cos(i1)/Vp1)).*(cos(j2)/Vs2)).*H.*p.^2)./D;
Related Question