Getting multiple outputs from a function?

Hi everyone. This is my code. It works well in this state but i want to change ''ord'' number from 1 to 4 and ''sfn'' number from 1 to ord+1. For example, when ord=3, sfn values should be 1, 2, 3 and 4, accordingly. How can i do this?
% shapefun( ord,sfn,x ) function gives the value of the 'sfn'th shape function of
% element with order 'order' at x coordinate.
% 'order' can be any integer above 0
% x € [-1,1]
% sfn is integers from 1 up to (order+1),
function result = shapefun(ord,sfn,x)
result=1;
if nargin==0
ord = 1;
sfn = 1;
x = linspace(-1,1,1000);
end
for i = 1:ord+1
if(i==sfn)
result = result.*1;
else
result = result.*(x-((i-1).*1./ord))./(((sfn-1).*1./ord)-((i-1).*1./ord));
end
end
end

1 comentario

Your code:
if(i==sfn)
result = result.*1;
Last I checked, multiplying by 1 doesn't do much. What is the point of that?

Iniciar sesión para comentar.

Respuestas (1)

Anish Navalgund
Anish Navalgund el 31 de Mzo. de 2019
Hi Isa,
You can use switch statements to control your order and for each order you can have for loops to calculate for each sfn.
The psuedo code is below.
order = input('order= ');
switch order
case 1
for sfn = 1:2
result = shapefun(ord,sfn,x);
end
case 2
for sfn = 1:3
result = shapefun(ord,sfn,x);
end
end
And so on...
Hope this helps.

6 comentarios

Stephen23
Stephen23 el 31 de Mzo. de 2019
Editada: Stephen23 el 31 de Mzo. de 2019
Simpler code does the same thing with much less effort:
for sfn = 1:order+1
result = shapefun(ord,sfn,x);
end
isa karaca's "Answer" moved here:
Anish Navalgund Thanks for answering but where exactly should i put your part into my code? Can you show that pls because i got 'not enough input' warning.
function result = shapefunn(ord,sfn,x)
result=1;
if nargin==0
x = linspace(-1,1,1000);
order = input('order= ');
switch order
case 1
for sfn = 1:2
result = shapefunn(ord,sfn,x);
end
case 2
for sfn = 1:3
result = shapefunn(ord,sfn,x);
end
end
end
for i = 1:ord+1
if(i~=sfn)
result = result.*(x-((i-1).*1./ord))./(((sfn-1).*1./ord)-((i-1).*1./ord));
end
end
end
Stephen23
Stephen23 el 31 de Mzo. de 2019
Editada: Stephen23 el 31 de Mzo. de 2019
@isa karaca: Anish Navalgund showed you a (rather complex) way of calling your function in a loop. They did not show you anything to "put into" your code.
Do you see these (repeated) lines in Anish Navalgund's code?:
result = shapefun(ord,sfn,x);
Those lines call your function. There is nothing here that gets "put into" your function.
isa karaca
isa karaca el 31 de Mzo. de 2019
I got it. I integrate that part into my code but i have another question. For example, when ord=1 and sfn= 1:2, my result matrix is being one row not two, i mean function take sfn as only 2 not both 1 and 2. Can you help about this case?
Stephen23
Stephen23 el 31 de Mzo. de 2019
Editada: Stephen23 el 31 de Mzo. de 2019
"...my result matrix is being one row not two, i mean function take sfn as only 2 not both 1 and 2. Can you help about this case?"
Most likely you will need to use indexing on the output array, otherwise you simply discard the results of every iteration except for the last one. I say "most likely" as I do not understand your algorithm.
Anish Navalgund
Anish Navalgund el 31 de Mzo. de 2019
@Isa, like @Stephen said you can use his method as the repetition of calling of the function is reduced. :)

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 31 de Mzo. de 2019

Comentada:

el 31 de Mzo. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by