why can't my 2nd output argument in my function return any value?

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

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

wht does 2 5 and 10 mean? so how do i correct my code?
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
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
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.
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
How are you calling your function? It works correctly when I run it.
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)
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
@Natalia — If my Answer solved your problem, please Accept it.
@Walter — Thank you.

Sign in to comment.

More Answers (0)

Categories

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