Effacer les filtres
Effacer les filtres

How to simulate T by inverse transform method

1 vue (au cours des 30 derniers jours)
Raushan
Raushan le 23 Jan 2024
function T01 = simulate_T01(lambda, mu1, n_simulations)
u = rand(1, n_simulations);
T01 = -1 / (lambda + mu1) * log(1 - ((lambda + mu1) / lambda) * u);
end
It gives NaN.
The cdf is defined P(T<t)=\frac{\lambda}{\lambda+\mu_1} \left( 1-e^{-(\lambda+\mu_1)t}\right). How to simulate t?
It gives NaN since u is from [0,1]

Réponse acceptée

Hassaan
Hassaan le 23 Jan 2024
lambda = 5; % Replace with your value for lambda
mu1 = 3; % Replace with your value for mu1
n_simulations = 1000; % The number of random values you want to simulate
% Call the function
T01 = simulate_T01(lambda, mu1, n_simulations);
% Display the simulated times
disp(T01);
Columns 1 through 20 0.0188 0.0623 0.1160 0.1132 0.0903 0.0226 0.0959 0.0234 0.0451 0.0647 0.0523 0.0388 0.0763 0.1083 0.0852 0.0573 0.0006 0.0327 0.0082 0.0083 Columns 21 through 40 0.0003 0.0850 0.0715 0.0009 0.0854 0.0075 0.0932 0.0827 0.1144 0.0285 0.0287 0.0884 0.1148 0.0979 0.0334 0.0523 0.1059 0.0265 0.0553 0.0576 Columns 41 through 60 0.1080 0.1079 0.0159 0.0120 0.0113 0.0119 0.0359 0.1031 0.0128 0.0058 0.0031 0.0197 0.0144 0.0509 0.0099 0.0777 0.0222 0.1053 0.0240 0.0479 Columns 61 through 80 0.0074 0.0250 0.0869 0.0429 0.1036 0.0469 0.0054 0.1151 0.0209 0.0550 0.0205 0.0683 0.0659 0.0972 0.0515 0.0930 0.1002 0.0370 0.0534 0.0507 Columns 81 through 100 0.1001 0.0805 0.0138 0.0323 0.0974 0.1076 0.0460 0.0031 0.1055 0.0766 0.0870 0.0674 0.1030 0.1202 0.0482 0.0504 0.0266 0.0152 0.0007 0.1198 Columns 101 through 120 0.1180 0.0973 0.1030 0.0372 0.0299 0.0398 0.0017 0.0151 0.0059 0.1201 0.0460 0.0402 0.0434 0.0695 0.0397 0.1095 0.0604 0.0200 0.0258 0.0006 Columns 121 through 140 0.0024 0.1090 0.0787 0.0842 0.0390 0.0668 0.0922 0.0560 0.0924 0.0126 0.1214 0.0429 0.0517 0.0177 0.0211 0.0351 0.0258 0.0400 0.0160 0.0057 Columns 141 through 160 0.0087 0.0351 0.1034 0.0032 0.0203 0.0053 0.0478 0.0026 0.0229 0.0691 0.0997 0.0435 0.0403 0.0040 0.0576 0.0425 0.1076 0.0346 0.0684 0.0279 Columns 161 through 180 0.0414 0.0833 0.0389 0.0095 0.0430 0.0975 0.0858 0.0648 0.0346 0.0821 0.0649 0.0043 0.0023 0.0168 0.0346 0.0234 0.0113 0.0589 0.0219 0.0871 Columns 181 through 200 0.0068 0.0116 0.0741 0.0485 0.0089 0.1175 0.0256 0.1063 0.0383 0.0934 0.0283 0.0627 0.0961 0.0216 0.0022 0.0415 0.0195 0.0156 0.0122 0.0903 Columns 201 through 220 0.0243 0.0084 0.1045 0.0889 0.0458 0.1021 0.0852 0.0562 0.0138 0.0254 0.0320 0.0316 0.0912 0.0338 0.1117 0.0359 0.0960 0.0909 0.0729 0.0361 Columns 221 through 240 0.0191 0.0515 0.0855 0.0719 0.1188 0.0098 0.0492 0.0186 0.0003 0.0394 0.0309 0.0987 0.0379 0.0657 0.0663 0.0391 0.0172 0.0903 0.0036 0.0770 Columns 241 through 260 0.0333 0.0860 0.0417 0.0125 0.0571 0.0663 0.0220 0.1211 0.1008 0.0137 0.0899 0.0066 0.0245 0.1121 0.0717 0.0422 0.0363 0.0303 0.0043 0.0006 Columns 261 through 280 0.0002 0.1103 0.1169 0.0047 0.0218 0.0190 0.1168 0.0354 0.0920 0.0675 0.0334 0.0007 0.0377 0.0442 0.0533 0.0468 0.0594 0.0205 0.0013 0.0025 Columns 281 through 300 0.0677 0.0518 0.0831 0.0092 0.0879 0.0376 0.0262 0.0311 0.0559 0.0109 0.0254 0.0445 0.0468 0.0873 0.0891 0.0142 0.0741 0.0675 0.0923 0.0093 Columns 301 through 320 0.0491 0.0694 0.0091 0.0039 0.0394 0.0651 0.0588 0.0675 0.0227 0.0472 0.0715 0.0699 0.0542 0.0031 0.0714 0.0683 0.0170 0.0681 0.0162 0.0728 Columns 321 through 340 0.0426 0.0964 0.0066 0.0214 0.0158 0.0326 0.0583 0.0605 0.0542 0.0419 0.0629 0.0708 0.0309 0.0669 0.0191 0.0473 0.0980 0.0697 0.0240 0.0107 Columns 341 through 360 0.0472 0.1080 0.0394 0.1214 0.0132 0.0895 0.1175 0.0568 0.0226 0.0357 0.0048 0.0147 0.0712 0.0674 0.0758 0.0317 0.0606 0.0074 0.0150 0.0455 Columns 361 through 380 0.0080 0.0458 0.0231 0.0416 0.0094 0.0464 0.0574 0.0505 0.0471 0.0967 0.0046 0.1038 0.0062 0.0596 0.1209 0.0703 0.0864 0.0107 0.0873 0.0781 Columns 381 through 400 0.0010 0.0030 0.0265 0.0429 0.0223 0.1098 0.0170 0.1079 0.0857 0.0628 0.0230 0.0093 0.0515 0.0185 0.0968 0.0469 0.0779 0.0687 0.1044 0.0276 Columns 401 through 420 0.0825 0.0601 0.1181 0.0250 0.0855 0.0713 0.0083 0.0102 0.1166 0.0577 0.0945 0.0902 0.0586 0.0654 0.0576 0.0853 0.0044 0.0552 0.0218 0.1167 Columns 421 through 440 0.1040 0.0005 0.0112 0.0211 0.0911 0.0000 0.0501 0.0155 0.0226 0.0189 0.0164 0.0164 0.0293 0.0197 0.0302 0.1223 0.0189 0.0586 0.0283 0.0663 Columns 441 through 460 0.0120 0.0133 0.1134 0.0825 0.0369 0.0257 0.0427 0.0392 0.0143 0.0142 0.0161 0.0549 0.0185 0.0571 0.0630 0.0735 0.1117 0.0314 0.0480 0.0461 Columns 461 through 480 0.0987 0.0728 0.0798 0.0421 0.0644 0.0525 0.0309 0.0278 0.0225 0.1035 0.0234 0.0741 0.0707 0.0608 0.0482 0.0785 0.0097 0.0375 0.0922 0.0428 Columns 481 through 500 0.0937 0.0329 0.0390 0.1070 0.0429 0.0783 0.0367 0.0058 0.0684 0.0291 0.0665 0.0044 0.1088 0.0864 0.1211 0.0139 0.0156 0.0963 0.0690 0.0318 Columns 501 through 520 0.0746 0.0318 0.0266 0.0399 0.0214 0.0229 0.0875 0.1064 0.0835 0.0202 0.0052 0.0851 0.0225 0.0265 0.0648 0.0389 0.0572 0.1039 0.0287 0.0902 Columns 521 through 540 0.0314 0.0728 0.0283 0.0663 0.0549 0.0102 0.0610 0.0374 0.0557 0.0506 0.0126 0.0188 0.0331 0.0826 0.0309 0.0160 0.0311 0.0796 0.0523 0.0111 Columns 541 through 560 0.0311 0.0803 0.0449 0.0089 0.0715 0.0887 0.0940 0.1171 0.0669 0.0189 0.0640 0.0218 0.0061 0.1024 0.0964 0.0812 0.0074 0.0650 0.0422 0.0778 Columns 561 through 580 0.1085 0.0467 0.1034 0.0012 0.0008 0.0210 0.0245 0.0211 0.0147 0.0721 0.0260 0.0178 0.0538 0.0299 0.0326 0.0939 0.0345 0.0290 0.0031 0.0638 Columns 581 through 600 0.1092 0.0346 0.0635 0.0884 0.0018 0.0704 0.1182 0.0115 0.0218 0.1062 0.0141 0.0405 0.1132 0.0430 0.0104 0.0293 0.1014 0.0301 0.0486 0.0082 Columns 601 through 620 0.0550 0.0923 0.0122 0.0446 0.1082 0.0791 0.0481 0.0641 0.0721 0.0803 0.0844 0.0848 0.0056 0.0166 0.0203 0.0154 0.0770 0.0672 0.0250 0.0657 Columns 621 through 640 0.0084 0.0164 0.0311 0.0693 0.0347 0.0434 0.0407 0.1164 0.0155 0.0227 0.0650 0.0575 0.1033 0.0165 0.0655 0.0939 0.0543 0.0935 0.0928 0.0445 Columns 641 through 660 0.0173 0.0986 0.0119 0.0562 0.0839 0.0641 0.1206 0.0178 0.0907 0.0346 0.0016 0.0151 0.0310 0.0511 0.0911 0.0190 0.0889 0.1223 0.0631 0.0776 Columns 661 through 680 0.0038 0.0018 0.1078 0.0339 0.0691 0.0419 0.0767 0.0378 0.0218 0.0158 0.0681 0.0473 0.0133 0.0145 0.0330 0.1170 0.0610 0.0173 0.0391 0.0122 Columns 681 through 700 0.0591 0.1123 0.0250 0.0417 0.1083 0.0387 0.0865 0.0090 0.0641 0.0244 0.0547 0.0095 0.0048 0.0191 0.0218 0.1153 0.0018 0.0107 0.0559 0.1137 Columns 701 through 720 0.0174 0.0113 0.0780 0.0553 0.0873 0.0684 0.0321 0.0829 0.0378 0.0207 0.1111 0.0322 0.0312 0.0047 0.1016 0.1065 0.0558 0.0004 0.0967 0.1201 Columns 721 through 740 0.0100 0.0259 0.0592 0.0406 0.0278 0.0207 0.0812 0.0687 0.0200 0.0169 0.0088 0.0913 0.0532 0.0203 0.0636 0.0053 0.0227 0.0699 0.0152 0.0154 Columns 741 through 760 0.1202 0.0979 0.0581 0.0899 0.0622 0.0834 0.0994 0.0482 0.0006 0.0110 0.1100 0.0132 0.0699 0.0645 0.0349 0.0305 0.0324 0.0069 0.0026 0.0439 Columns 761 through 780 0.0017 0.0976 0.0536 0.0062 0.0129 0.0305 0.0010 0.0545 0.0660 0.0095 0.0928 0.0194 0.0160 0.0767 0.0442 0.0077 0.0242 0.0659 0.0603 0.0620 Columns 781 through 800 0.0219 0.0509 0.0263 0.0447 0.0221 0.0457 0.0145 0.0424 0.1115 0.0531 0.0708 0.0477 0.0486 0.0113 0.0458 0.0828 0.0598 0.0066 0.0810 0.0373 Columns 801 through 820 0.0923 0.0676 0.0064 0.0832 0.0624 0.0053 0.0049 0.0088 0.0806 0.1001 0.0360 0.0213 0.0338 0.0947 0.0310 0.0624 0.0687 0.0984 0.0121 0.0287 Columns 821 through 840 0.0559 0.0104 0.1037 0.0147 0.0794 0.0270 0.0131 0.0475 0.0007 0.0197 0.0668 0.0134 0.0023 0.0434 0.0265 0.0709 0.1138 0.0231 0.0268 0.0874 Columns 841 through 860 0.0329 0.0186 0.0363 0.0577 0.0584 0.0502 0.0210 0.0270 0.0684 0.0197 0.0987 0.0421 0.0451 0.0837 0.1049 0.1127 0.1018 0.0251 0.0077 0.1134 Columns 861 through 880 0.0710 0.0142 0.0212 0.0096 0.0638 0.0030 0.0334 0.0117 0.0236 0.0936 0.0626 0.0070 0.0561 0.0379 0.0398 0.0206 0.0319 0.1157 0.0168 0.0526 Columns 881 through 900 0.0149 0.0547 0.0168 0.1078 0.0584 0.0288 0.0135 0.0414 0.0327 0.0197 0.1005 0.0322 0.1041 0.0445 0.0983 0.0109 0.0011 0.0093 0.0514 0.0175 Columns 901 through 920 0.0015 0.0507 0.1155 0.0282 0.0210 0.1006 0.1084 0.0548 0.0290 0.0341 0.0241 0.0778 0.0114 0.1125 0.0569 0.1088 0.0420 0.0837 0.0394 0.1135 Columns 921 through 940 0.0690 0.0213 0.0701 0.0108 0.0336 0.0047 0.0784 0.0675 0.0464 0.0510 0.0882 0.0241 0.0127 0.0525 0.1166 0.1176 0.0622 0.0317 0.0132 0.0263 Columns 941 through 960 0.0019 0.1198 0.1138 0.0565 0.1135 0.0637 0.1117 0.0004 0.0077 0.0272 0.0033 0.1075 0.0586 0.0556 0.0738 0.0347 0.0227 0.0876 0.0136 0.0380 Columns 961 through 980 0.0390 0.0859 0.0140 0.0960 0.0807 0.0120 0.0291 0.0786 0.0754 0.0967 0.0917 0.0900 0.0342 0.0155 0.0011 0.0335 0.0236 0.0869 0.0591 0.0633 Columns 981 through 1,000 0.0467 0.0630 0.0262 0.0122 0.0431 0.1167 0.1138 0.0764 0.0665 0.0709 0.0803 0.0504 0.0717 0.0337 0.0480 0.1178 0.0546 0.0296 0.0321 0.0886
% Plot the data
figure; % Creates a new figure window
plot(T01, 'o-'); % Plot T01 with circle markers connected by lines
title('Simulated Times T01');
xlabel('Simulation Index');
ylabel('Time');
grid on; % Adds a grid to the plot for better readability
function T01 = simulate_T01(lambda, mu1, n_simulations)
% Ensure that lambda and mu1 are positive to avoid complex numbers
if lambda <= 0 || mu1 <= 0
error('Lambda and mu1 must be positive.');
end
% Scale u to the range of [0, lambda/(lambda + mu1)]
u = rand(1, n_simulations) * (lambda / (lambda + mu1));
% Apply the inverse CDF transform to get T01
T01 = -1 / (lambda + mu1) * log(1 - u);
end
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Plus de réponses (0)

Catégories

En savoir plus sur Simscape Electrical 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