How to use function return in Scilab or Matlab?

Asked

Viewed 1,947 times

0

I’m doing a simple numerical program.

But I can’t use return of functions in Scilab someone can help me?

The function has to solve a calculation and then return the value for this variable to continue to be manipulated by the program.

The main function is integral()

function funcao (x)
         x^2
         return F
endfunction

a=2
b=4
function integral()
    x=(a+b)/2 *0,222
    funcao(x)
    disp(F)
endfunction

2 answers

1

Matlab functions can have a single output or multiple. A single output:

function y = media(x)
if ~isvector(x) %testa se é um vetor
    error('A entrada tem que ser um vetor')
end
y = sum(x)/length(x); 
end

Multiple exits:

function [m,s] = stat(x)
n = length(x);
m = sum(x)/n;
s = sqrt(sum((x-m).^2/n)); %calcular o desvio
end

How to use:

valores = [12.7, 45.4, 98.9, 26.6, 53.1];
[ave,stdev] = stat(valores)

The way out would be:

ave =
   47.3400
stdev =
   29.4124

Your problem would be something like this:

a=2
b=4

function F = integral(a,b)

    x = (a+b)/2*0.222
    F = funcao(x)

end

function y = funcao (x)

         y = x^2
end

For more information see the reference here.

0

I don’t know anything about Matlab, but I think your show should be like this:

function funcao (x)
    return x ^ 2
endfunction

a = 2
b = 4

function integral()
    x = (a + b) / 2 * 0.222
    f = funcao(x)
    disp(f)
endfunction

Browser other questions tagged

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