How to call a concrete number of a vector inside a function

5 views (last 30 days)
Hi,
I have a position vector: x= [ 1 2 3 4 5 6 7] and an area vector A= [10 11 12 13 14 15]
I am trying to create a function called:
function [M] = Area_jumps (A1, A2, c1, c2, rho1, rho2, x1, s)
a = A2/A1.*rho1.*c1/(rho2.*c1);
t1 = s*x1./c1;
t2 = s*x1./c2;
M = [-exp(-t1), -exp(t1), +exp(-t2), +exp(t2);
-exp(-t1), +exp(t1), +a.*exp(-t2), -a.*exp(t2)];
end
% (where c is the speed of sound and rho is the density)
A1 and A2 represent the position, 1 and 2. But I want to do this for the 5th first position of the vector.
How can I do this?
Thank you

Answers (1)

Steven Lord
Steven Lord on 15 May 2020
It's not completely clear to me how the variables A1, A2, and x1 in your function are related to your A and x vectors. It's also not clear what "the 5th first position of the vector" means.
But taking an educated guess at where the problem lies, you may want to use element-wise division between A2 and A1.
A2 = 10:16;
A1 = 1:7;
sol1 = A2./A1 % sol1(k) is A2(k) divided by A1(k)
sol2 = A2/A1 % least-squares solution to sol2*A1 = A2, so a scalar
  3 Comments
Steven Lord
Steven Lord on 15 May 2020
A = 10:15;
m = 2;
p = 1:m+3;
k = p+1;
I think this is what you're looking for.
ratio = A(k)./A(p) % Note the ./ instead of /
Marina Fargas Canas
Marina Fargas Canas on 15 May 2020
I have defined the vector A, x, c, rho in another function, this why I have them in the inputs. The vectors are not the ones I wrote they are a bit more complex. They are:
x= [0; 0.0858; xm11; xm12; xm13; xm14; xm15; 0.0282; 0.2524; xm21;
xm22; xm23; xm24; xm25; 0.0424; 0.1787; 0.059;];
A = [Ax1 A_ch1 Ax2 Ax3 A_ch2 Ax4 Ax5];
%A_ch1 and A_ch2 have been calculated previously
c = [c1 cm c1 c2 c2];
rho = [rho1 rhom rho1 rho2 rho2];
%cm and rhom have been also calculated before.
When I try to see the values of M it doesn't give me results and I don't understand why. Shall I store the values of M? shall I make a loop?
I am a bit lost with this.
At the end I want the Matrix M with all the numbers of the loop.

Sign in to comment.

Categories

Find more on Parallel Computing Toolbox 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!