why can't my 2nd output argument in my function return any value?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Hi, I 'm trying to create a function that calculates the root from the the quadratic eqn. i need to display the two roots separately but why does my second root, k not appear from the output.argument. thanks i am new to matlab
Accepted Answer
Star Strider
on 4 Dec 2015
You have to ask it specifically for as many outputs as you want it to return. By default, if you only ask it for one, it will only return the first. This call returns all of them:
a = 2;
b = 5;
c = 10;
[j,k,group]=quadratic_roots(a,b,c)
Also, I would change:
group='non-equal complex valued roots'
to:
group='complex-conjugate roots'
11 Comments
Natalia Wong
on 4 Dec 2015
wht does 2 5 and 10 mean? so how do i correct my code?
Natalia Wong
on 4 Dec 2015
i did what you said but i didnt get the answer i wanted if i input(2,-4,1) i should get 1.71 and 0.2929 as ans. I'm required to use non equal complex roots as group Thanks
Natalia Wong
on 4 Dec 2015
i want j to be the first root and k to be the second root. How to make them appear separately liek j = 1.71 and k = 0.2929
Star Strider
on 4 Dec 2015
Your code works for me. If I use:
a = 2;
b = -4;
c = 1;
[j,k,group]=quadratic_roots(a,b,c)
I get:
group =
non-equal real valued roots
j =
0.29289
k =
1.7071
group =
non-equal real valued roots
You need to put semicolons at the end of your ‘group’ lines to suppress the output:
group='non-equal real valued roots';
and so for the others.
Natalia Wong
on 4 Dec 2015
if i dont sppress j and k i can get the answer but then the answer in the output argument gives me 0.2929 only it appears like this j = 0.2929 k = 1.7071 group = non equal real valued roots ans = 0.2929
Star Strider
on 4 Dec 2015
How are you calling your function? It works correctly when I run it.
Natalia Wong
on 4 Dec 2015
quadratic_roots(2,-4,1)
Star Strider
on 4 Dec 2015
Edited: Star Strider
on 4 Dec 2015
That will only return the first output and will assign it to the default variable, ‘ans’.
Call it like this instead:
[j,k,group]=quadratic_roots(a,b,c)
or with your particular arguments:
[j,k,group]=quadratic_roots(2,-4,1)
Natalia Wong
on 5 Dec 2015
Edited: Walter Roberson
on 5 Dec 2015
Thank you so much Star Strider. Since you're so good can you help me with this task? I'm supposed to create a program that prompts the user to guess the colour (5 characters) and tell them how many colours and position that that they guessed are correct and that how many colours only that they guessed are correct. Pls help I'm stuck. If result == hidden_seq gives me 1 0 0 0 0 means that the first colour and first position is correct but how do I tell that to the user since the hidden sequence varies and the user enters a different different seq of colours everytime?
%Written by:
% Student ID:
%ver 01, 8 Dec by JL
clc
clear
% Initiate Random Color Sequence
% r-red; b-blue; y-yellow; g-green; w-white; p-purple; o-orange;
all_colors = 'rbygwpo';
index = randperm(7);
hidden_seq = all_colors(index(1:5));
count = 0;
while 1
%Prompting user to enter input
result = input('Guess the 5 characters colour sequence: ');
count = count + 1;
%Validate user input
if numel(result) > 5 || numel(result) < 5
error('You have entered more or less than 5 characters')
end
%Check the user guesses
result == hidden_seq
end
Walter Roberson
on 5 Dec 2015
That is already being dealt with in your question http://uk.mathworks.com/matlabcentral/answers/258976-i-m-having-problems-creating-a-program-that-prompts-a-user-to-guess-the-colour
Star Strider
on 5 Dec 2015
@Natalia — If my Answer solved your problem, please Accept it.
@Walter — Thank you.
More Answers (0)
Categories
Find more on Function Creation in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)