bug in sprintf 2024a, can't use other variable and just repeating the first variable

this is the code that spoiled my food, I've been following this tutorial and I got stuck at this one for TWO HOURS! I can't use the second variable!
Nume = app.NumeratorEditField.Value;
Domi = app.DenominatorEditField.Value;
[r,p,k] = residue(Nume,Domi); % this is for partial fraction
r1 = real(r) %I am trying to seperate the real # from the imaginary number (complex)
r2 = imag(r) %I am trying to seperate the real # from the imaginary number (complex)
app.rTextArea.Value = sprintf('%g + %gi\n',r1, r2) %this is where I am stuck
%%% Instead of giving me the result:
0.23123 + -1.123123i
0.344 + 1.7766i
0.234 + 0.00251234i
%%% It is giving me this:
0.23123 + 0.23123i
0.344 + 0.344i
0.234 + 0.234i
-1.123123 + -1.123123i
1.7766 + 1.7766i
0.00251234 + 0.00251234i
Take note about the bold&italic numbers this might be clue ;(
PLEEASE HELP ME PASSED THIS COURSE ToT

 Accepted Answer

The residue funciton produces column vectors in its output. To use sprintf with them, concatenate them in a matrix and then transpose the matrix:
sprintf('%g + %gi\n',[r1, r2].')
Try this —
Nume = poly(randi(1,3))*10
Nume = 1×4
10.0000 -30.0000 -0.0000 -0.0000
Domi = poly(randi(1,4))*10
Domi = 1×5
10.0000 -40.0000 -0.0000 -0.0000 -0.0000
[r,p,k] = residue(Nume,Domi); % this is for partial fraction
r1 = real(r) %I am trying to seperate the real # from the imaginary number (complex)
r1 = 4×1
0.2500 0.7500 0.0000 -0.0000
r2 = imag(r) %I am trying to seperate the real # from the imaginary number (complex)
r2 = 4×1
0 0 0 0
sprintf('%g + %gi\n',[r1, r2].') %this is where I am stuck
ans =
'0.25 + 0i 0.75 + 0i 2.70696e-16 + 0i -3.58866e-32 + 0i '
.

More Answers (0)

Categories

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!