How do I assign a variable a specific scalar value from a matrix?
Afficher commentaires plus anciens
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
Plus de réponses (1)
Image Analyst
le 17 Jan 2015
0 votes
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
le 17 Jan 2015
Catégories
En savoir plus sur Whos dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!