How to return output argument in Matlab?

Asked

Viewed 231 times

0

I created a function to calculate a linear system with 5 unknowns (A1 to A5), which has as an output argument 'Ai' and input 'A and b' which are respectively a Matrix and Vector. The values of A and b are assigned, but I did not put here.

At the end of the function I get the Ai values (which are 5). However I need to use the Ai values outside of my function!

FOLLOWS BELOW THE FUNCTION:

function Ai = gauss(A,b)

%% dimensao da matriz. linha, coluna

[l,c]=size(A); 

%% eliminação

for i=1:l-1

for j=i+1:l;

%% definição da constante para zerar as linhas

constante=A(j,i)/A(i,i); 

for k=i+1:l;

%% operação linear para eliminar as linhas

A(j,k)=A(j,k)-constante*A(i,k); 

end

A(j,i)=0;

b(j)=b(j)-constante*b(i);

end

end  

%% Resolução do sistema

%% função Ai recebe um array de zeros

Ai=zeros(l,1);

fprintf('SOLUÇÃO DO SISTEMA:\n\n');

for i=l:-1:1;

    soma=0;

    for k=i+1:l;

        soma=soma+A(i,k)*Ai(k);

    end

    Ai(i)=(b(i)-soma)/A(i,i);

end

end

y=x^2+x;

I want to multiply each value of Ai (that is 5) by the expression below that is outside the function Gauss.

y=x^2+x;

But how to use Ai values outside the function?

1 answer

0

Input and output is very simple in Matlab. But to answer you, you need to put where you are using Ai and how you’re calling your job.

After all, if you are using the function to calculate something, you need to call it and input values A and b in some way. And it will inevitably come out of the values of Ai, that you may be suppressing.

If you have doubts about how to work with Matlab, Matlab website has good features and several examples. Maybe it’s an idea to take a look before embarking on larger projects. In addition, a quick search in google returns a lot of material, but in English has much more resources

  • I am using Ai within the function 'Gauss'. Yes, I am using the function to calculate the unknowns of a linear system.

  • Obg for the tips

  • @Student you are creating (and maybe using) Ai within the function gauss, but by your description you want to use these values outside the Gauss function. Is this part out of gauss I need to see your problem.

  • THE COMPLETE FUNCTION IS:

  • Ask your question. It’s clearer this way.

  • READY GUTO, THE QUESTION HAS BEEN EDITED!!!!! I HOPE IT IS CLEAR NOW

  • @Student is not completely clear yet. You put as you want to use, but not as you are using. Where to function gauss is it called? With what values to test? Use a [mcve], this helps in solving the problem. For example, your function is not working with matrices larger than 1 dimension!

  • @Student I must say that the function gauss in itself is not necessary to solve your problem. A [mcve] can be obtained without the 'resolution' part of the function, only with a Matrix within any function. This still doesn’t solve the problem I asked you: how you are calling the function (be it Gauss or a simple example with a function that has a Matrix that you want to save).

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.