How to use just one output out of a function with several outputs in an equation?

4 views (last 30 days)
I have a function with three outputs depending on x. I want to use one of these three say B(x) in an equation:
function [ A,B,C ] = f( x )
Equation=1+x+B(x)
How do I have to code the equation? what I want to have is something like this (does not work):
Equation=1+x+f(B(x))

Answers (1)

Jan
Jan on 11 Nov 2017
Edited: Jan on 11 Nov 2017
You have to do this in two lines:
[~, B] = f(x);
Equation = 1 + x + B(x);
Of course you could write a function, which extracts a certain output only, but this is less clear:
function X = SelectOutput(N, Fcn, varargin)
[Out{1:N}] = Fcn(varargin{:});
X = Out{N};
end
Then:
Equation = 1 + x + SelectOutput(2, f(x));
I don't think, that this is an advantage.
  2 Comments
Stef
Stef on 11 Nov 2017
But why should I write a function then? (I have the task to do so) Because then I could just do it like this:
b=@(x) 3*x
Jan
Jan on 12 Nov 2017
I cannot follow you. This code is unclear already:
function [ A,B,C ] = f( x )
Equation=1+x+B(x)
Why does "B" appear in the output, when it is used inside the function? Where do A and C come from?
Then "b=@(x) 3*x" seems to be completely unrelated. Please start from scratch and explain, which problem you try to solve. Append this information by editing the original question.

Sign in to comment.

Categories

Find more on Programming 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!