How to create this function in Matlab?

I want to write a mathematical function in matlab so that I can call it in my program whenever needed.
? a(n).b(n)/sqrt((a(n)^2) + (b(n)^2))
n=0 to limit. The limit is approx 1000.
a and b are 1 dimensional matrix. size of a and b is : (1xlimit)

댓글 수: 6

Matt Fig
Matt Fig 2011년 3월 25일
What is this: a(n).b(n)
is that supposed to be: a(n)*b(n)
Walter Roberson
Walter Roberson 2011년 3월 25일
Is the '?' the summation operator?
vish
vish 2011년 3월 26일
Sorry, the '?' is a summation operator. Its still showing me the operator but I believe the symbol is not recognizable in other computers.
summation of n=0 to limit...
summation of a(n)*b(n)/(sqrt((a(n)^2) + (b(n)^2)))
limit is the size of matrix a and b.
vish
vish 2011년 3월 26일
I will try to explain in a better way. if x= 4 then i want to do
a(1)*b(1)/(sqrt((a(1)^2) + (b(1)^2))) + a(2)*b(2)/(sqrt((a(2)^2) + (b(2)^2))) + a(3)*b(3)/(sqrt((a(3)^2) + (b(3)^2))) + a(4)*b(4)/(sqrt((a(4)^2) + (b(4)^2))).
In my actual program the value of x is more than 1000, so its not possible for me to do it manually. I am sorry I cudnt explain properly earlier.
a and b are 1x4 matrix
Matt Fig
Matt Fig 2011년 3월 26일
Did you try the codes below?
vish
vish 2011년 3월 26일
Thanks, I just tried it. Its working. Matlabs really powerful. I am grateful for showing me the snippet.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2011년 3월 25일

0 개 추천

Assuming the '?' is the summation operator, then:
sum(a.*b./sqrt(a.^2 + b.^2))

댓글 수: 1

Matt Fig
Matt Fig 2011년 3월 25일
Beat me too it! I was too slow deciphering the question, I guess.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt Fig
Matt Fig 2011년 3월 25일

0 개 추천

If I understand you correctly,
function S = mysum(a,b)
S = sum(a.*b./sqrt(a.^2 + b.^2));
EDIT
.
. Sean de points out known stability issues with square roots of the sum of squares.
function S = mysum(a,b)
S = sum(a.*b./hypot(a,b));

댓글 수: 2

Sean de Wolski
Sean de Wolski 2011년 3월 25일
I would use:
./hypot(a,b)
Matt Fig
Matt Fig 2011년 3월 25일
That too...

댓글을 달려면 로그인하십시오.

제품

질문:

2011년 3월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by