How do I assign a variable a specific scalar value from a matrix?

42 vues (au cours des 30 derniers jours)
mentalfiction
mentalfiction le 16 Jan 2015
Commenté : Star Strider le 17 Jan 2015
Hey guys so I'm trying to run this program in which I have two calculated values based on a range of values that the user inputs, and I want to isolate the number which will yield the difference of the two calculated values to be zero. Now I'm not a professional at Matlab so excuse my coding, but this is what it looks like:
Mcr1=0;
Mcr2=0;
y=1.4;
x=0;
Mcr1=input('Enter a minimum value for the Critical Mach Number: ');
Mcr2=input('Enter a maximum value for the Critical Mach Number: '); Mcr=[Mcr1:0.0001:Mcr2];
CPcr1=(-0.43)./(sqrt(1-(Mcr).^2));
CPcr2=(2./(y.*(Mcr).^2)).*(((1+((y-1)/2).*(Mcr).^2)/(1+(y-1)/2)).^(y/(y-1))-1);
CPcr=CPcr1-CPcr2;
CP=abs(CPcr);
fprintf('\n')
disp(' Mcr CPcr1 CPcr2 Difference')
table=[Mcr',CPcr1',CPcr2',CPcr'];
disp(table)
while x<10;
*act_diff=str2num(sprintf('%.4f',CP));
if act_diff<=0.0001
fprintf('The Mach number %.4f is the critical Mach number',Mcr)*
break
else
x=menu('There was no definite value of the critical Mach number. Would you like to calculate again with different values?','Yes','No');
switch x
case 1
continue
case 2
break
end
end
end
Now the part of the code in which I'm experiencing difficulties is within the asterisks.
fprintf('The Mach number %.4f is the critical Mach number',Mcr)
However Mcr is a vector, but I only need the SINGLE number (or even the two or three, however in that case my fprintf command should also be changed I think) in which the difference is as specified (less than 0.0001)
How do I get Matlab to isolate the value of Mcr from the range that has been input for which CP is less than 0.0001?
Thanks for the help!

Réponse acceptée

Star Strider
Star Strider le 16 Jan 2015
I just ran your code from the ‘Mcr’ assignment to the ‘CP’ assignment. I have no idea what values ‘Mcr1’ and ‘Mcr2’ are supposed to be, so I just made some up to test it.
If you want the minimum value of ‘CP’, the min function — with two outputs — will give you the minimum value and its index in the vector, so you can use the index to get the corresponding ‘Mcr’ value:
[CPmin,idx] = min(CP);
You might want to put this just after the ‘CP’ assignment. It could make your subsequent code more efficient and easier to work with.
By the way, if you are using R2013b or later, consider changing the name of your ‘table’ variable. The table function creates a table (something you might want to do, actually), and naming it as a variable will cause problems if you want to use the table function later in your code.
Also, consider replacing your input statements with inputdlg. It keeps Command Window clutter to a minimum. Note that the assignments from inputdlg are cells, so you have to add a line to convert them to double, but that’s relatively trivial.
  4 commentaires
mentalfiction
mentalfiction le 17 Jan 2015
I realized if I use the minimum value of CP, I don't need to enter a while loop. I've edited the program with what you suggested and it works perfect. Thank you so much once again!
Mcrmin=0;
Mcrmax=0;
gamma=1.4;
Mcrmin=input('Enter a minimum value for the Critical Mach Number: ');
Mcrmax=input('Enter a maximum value for the Critical Mach Number: ');
Range=[Mcrmin:0.0001:Mcrmax];
CPcr1=(-0.43)./(sqrt(1-(Range).^2));
CPcr2=(2./(gamma.*(Range).^2)).*(((1+((gamma-1)/2).*(Range).^2)/(1+(gamma-1)/2)).^(gamma/(gamma-1))-1);
Diff=CPcr1-CPcr2;
CP=abs(Diff);
[CPmin,idx] = min(CP);
Mcr = Range(idx);
fprintf('\n')
disp(' Mcr CPcr1 CPcr2 Difference')
table=[Range',CPcr1',CPcr2',Diff'];
disp(table)
fprintf('The Mach number %.4f is the critical Mach number',Mcr)
Star Strider
Star Strider le 17 Jan 2015
My pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 17 Jan 2015
In your while loop, what changes? Nothing as far as I can see. You should rename "x" to buttonNumber because that's what it is. People will initially think it has something to do with your y value, but it doesn't. Anyway, in your while loop you just keep looping without ever changing CP or Mcr - why????? Why do you think anything will ever change that will let you break out of the loop by causing act_diff<=0.0001????
And of course Mcr is a vector - that's what you programmed up so I not sure what you're expecting or asking when you say "I only need the SINGLE number (or even the two or three". I don't know what your plans for it are but you can just index it like Star showed you to pull out 1, 2, or 3 values for Mcr.
  1 commentaire
mentalfiction
mentalfiction le 17 Jan 2015
Oh you're right! I should put the input into the while loop. I didn't notice that because I was focusing on fixing the other issue. Thanks for pointing it out.
And what I was trying to find was the value of Mcr for which CPcr1 = CPcr2 (which will be when their difference = 0 or at least approaches zero).
As for the naming the variables, this is just a rough version, so once I can fix the problems I'm facing I'll rename all the variables to make it more cohesive.
Thanks for answering!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Variables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by