I learned photoshop CS2 recently. Here is a wallpaper I made. Please feel free to download it and use it as your desktop.
MATLAB applications, tutorials, examples, tricks, resources,...and a little bit of everything I learned ...
Sunday, December 19, 2010
Wednesday, October 20, 2010
solve linear equation system
To solve a linear equations system:
x+y+z=3
2x+2y+z=5
4x+8y-3z=9
linsolve is the function to use.
a=[1 1 1; 2 2 1; 4 8 -3];
b=[3;5;9];
linsolve(a,b)
Done!
x+y+z=3
2x+2y+z=5
4x+8y-3z=9
linsolve is the function to use.
a=[1 1 1; 2 2 1; 4 8 -3];
b=[3;5;9];
linsolve(a,b)
Done!
Tuesday, October 19, 2010
roots function and solve functioin
Today I learned two methods to solve polynomial.Two functions, roots and solve can be used. For example, if I would like to solve the polynomial:
x^3+2*x^2-8*x+10=0
I can use:
r=roots([1 2 -8 10])
or:
solve('x^3+2*x^2-8*x+10')
However, the answers will be given in different form. The first one will give numerical values of the roots, like:
ans =
-4.3605
1.1803 + 0.9488i
1.1803 - 0.9488i
the latter one will give symbolic form answers:
ans =
-1/3*(215+3*2697^(1/2))^(1/3)-28/3/(215+3*2697^(1/2))^(1/3)-2/3
1/6*(215+3*2697^(1/2))^(1/3)+14/3/(215+3*2697^(1/2))^(1/3)-2/3+1/2*i*3^(1/2)*(-1/3*(215+3*2697^(1/2))^(1/3)+28/3/(215+3*2697^(1/2))^(1/3))
1/6*(215+3*2697^(1/2))^(1/3)+14/3/(215+3*2697^(1/2))^(1/3)-2/3-1/2*i*3^(1/2)*(-1/3*(215+3*2697^(1/2))^(1/3)+28/3/(215+3*2697^(1/2))^(1/3))
Thursday, October 7, 2010
I was using a lot of greek symbols in my Word document, and usually when I need one, I input a English letter, for example, "a", then highlight it and change the font to symbol. That way, I will have a "α" there. However, if someone changed the font of that letter, it won't show up correctly- a blank will be left there instead of the letter you want.
I did some research and found one way to solve this problem: Firstly type in the unicode for the greek letter, then press "Alt+X". The unicode will be turned into a greek letter, and changing the font will not cause any trouble. The greek letter will show up anyway, just look a little different when different font is used.
And here is the reference for the unicode. This is very cool!
0391 Α GREEK CAPITAL LETTER ALPHA
0392 Β GREEK CAPITAL LETTER BETA
0393 Γ GREEK CAPITAL LETTER GAMMA
0394 Δ GREEK CAPITAL LETTER DELTA
0395 Ε GREEK CAPITAL LETTER EPSILON
0396 Ζ GREEK CAPITAL LETTER ZETA
0397 Η GREEK CAPITAL LETTER ETA
0398 Θ GREEK CAPITAL LETTER THETA
0399 Ι GREEK CAPITAL LETTER IOTA
039A Κ GREEK CAPITAL LETTER KAPPA
039B Λ GREEK CAPITAL LETTER LAMDA
039C Μ GREEK CAPITAL LETTER MU
039D Ν GREEK CAPITAL LETTER NU
039E Ξ GREEK CAPITAL LETTER XI
039F Ο GREEK CAPITAL LETTER OMICRON
03A0 Π GREEK CAPITAL LETTER PI
03A1 Ρ GREEK CAPITAL LETTER RHO
03A3 Σ GREEK CAPITAL LETTER SIGMA
03A4 Τ GREEK CAPITAL LETTER TAU
03A5 Υ GREEK CAPITAL LETTER UPSILON
03A6 Φ GREEK CAPITAL LETTER PHI
03A7 Χ GREEK CAPITAL LETTER CHI
03A8 Ψ GREEK CAPITAL LETTER PSI
03A9 Ω GREEK CAPITAL LETTER OMEGA
03AA Ϊ GREEK CAPITAL LETTER IOTA WITH DIALYTIKA
03AB Ϋ GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA
03B1 α GREEK SMALL LETTER ALPHA
03B2 β GREEK SMALL LETTER BETA
03B3 γ GREEK SMALL LETTER GAMMA
03B4 δ GREEK SMALL LETTER DELTA
03B5 ε GREEK SMALL LETTER EPSILON
03B6 ζ GREEK SMALL LETTER ZETA
03B7 η GREEK SMALL LETTER ETA
03B8 θ GREEK SMALL LETTER THETA
03B9 ι GREEK SMALL LETTER IOTA
03BA κ GREEK SMALL LETTER KAPPA
03BB λ GREEK SMALL LETTER LAMDA
03BC μ GREEK SMALL LETTER MU
03BD ν GREEK SMALL LETTER NU
03BE ξ GREEK SMALL LETTER XI
03BF ο GREEK SMALL LETTER OMICRON
03C0 π GREEK SMALL LETTER PI
03C1 ρ GREEK SMALL LETTER RHO
03C2 ς GREEK SMALL LETTER FINAL SIGMA
03C3 σ GREEK SMALL LETTER SIGMA
03C4 τ GREEK SMALL LETTER TAU
03C5 υ GREEK SMALL LETTER UPSILON
03C6 φ GREEK SMALL LETTER PHI
03C7 χ GREEK SMALL LETTER CHI
03C8 ψ GREEK SMALL LETTER PSI
03C9 ω GREEK SMALL LETTER OMEGA
03D1 ϑ GREEK THETA SYMBOL
03D2 ϒ GREEK UPSILON WITH HOOK SYMBOL
03D5 ϕ GREEK PHI SYMBOL
03D6 ϖ GREEK PI SYMBOL
03DC Ϝ GREEK LETTER DIGAMMA
03F0 ϰ GREEK KAPPA SYMBOL
03F1 ϱ GREEK RHO SYMBOL
Wednesday, September 29, 2010
How to install a toolbox in MATLAB
Today I learned how to install a toolbox in MATLAB. i know it's easy and simple for those who are experienced. But for me, it still took me a while to figure it out.
I downloaded a midi toolbox as a .zip file. Then I extracted it to one folder. The manual told me I need to "define the path varialbe", but how? Finally I knew it: File -> Set folder. One the pop-up dialogue box, use Add folder to add the path for the toolbox. Done!
I downloaded a midi toolbox as a .zip file. Then I extracted it to one folder. The manual told me I need to "define the path varialbe", but how? Finally I knew it: File -> Set folder. One the pop-up dialogue box, use Add folder to add the path for the toolbox. Done!
Use Audacity to change size of MP3 files
Today I used Audacity to edit some MP3 files so that they can be used as my cell phone ringtones. I opened the MP3 files in Audacity, cut out the fragment I liked, and exported them as MP3 files. After I send them to my cellphone via bluetooth, I found that the files are too large to be set as ringtones.
I checked online and on the Audacity forum I found that people are talking about export 'options', but in this case, the 'option' dialogue box didn't show up at all. I guess it is because what I imported is already a MP3 file and the bitrate has been set. Then I tried several different setting at different places. Finally I figured out that the right thing to set is the 'project rate'. Decreasing this project rate results in much smaller MP3 file.
I checked online and on the Audacity forum I found that people are talking about export 'options', but in this case, the 'option' dialogue box didn't show up at all. I guess it is because what I imported is already a MP3 file and the bitrate has been set. Then I tried several different setting at different places. Finally I figured out that the right thing to set is the 'project rate'. Decreasing this project rate results in much smaller MP3 file.
Saturday, September 25, 2010
Contam is also fun!
Today I happened to find out that NIST has developed some software and they are free. So I downloaded one of them, Contam 2.4, followed the tutorials and finished my first ever Contam project. It's fun, though I am not sure how accurate the results could be.
Here's the link for the software:
http://www.bfrl.nist.gov/IAQanalysis/index.htm
And this is my results:
Here's the link for the software:
http://www.bfrl.nist.gov/IAQanalysis/index.htm
And this is my results:
Wednesday, August 25, 2010
Generate an input data array to train the neural network
A simple loop-in-loop program.
%matlab code
clc; clear all; close all;
A=[0.1, 0.2, 0.3, 0.1, 0.1, 0.2]; % assumed initial value
B=[0.5, 0.1, 0.1, 0, 0.2, 0.1];
C=[0, 0.2, 0.2, 0.4, 0.1, 0.1];
final=[];
r=1;
for i= 0:10
for j=0:10
for k=1:10
sum_up=(i*A+j*B+k*C);
total=sum(sum_up);
result=sum_up/total;
row=[i, j, k, result];
final(r,:)=row;
r=r+1;
end
end
end
%matlab code
clc; clear all; close all;
A=[0.1, 0.2, 0.3, 0.1, 0.1, 0.2]; % assumed initial value
B=[0.5, 0.1, 0.1, 0, 0.2, 0.1];
C=[0, 0.2, 0.2, 0.4, 0.1, 0.1];
final=[];
r=1;
for i= 0:10
for j=0:10
for k=1:10
sum_up=(i*A+j*B+k*C);
total=sum(sum_up);
result=sum_up/total;
row=[i, j, k, result];
final(r,:)=row;
r=r+1;
end
end
end
Thursday, August 12, 2010
Download Matlab toolbox manual - Fuzzy logic toolbox
I would like to share a good book to show you how to use Fuzzy Logic Toolbox in Matlab. You can download it from:
http://www.box.net/shared/xe94kj9ej1
Enjoy!
http://www.box.net/shared/xe94kj9ej1
Enjoy!
Monday, August 2, 2010
How to import JPG/JPEG image into AutoCAD 2009?
It's very hard to find the right button on the menu (or navigation panel ?)of AutoCAD 2009. So just type the command:
IMAGEATTACH
That's it!
A GREAT way to reduce the .TIF file size while keep same high quality
Use Microsoft Office Picture Manager to open the original .TIF file, then export, set export size to 100%, DONE!
I had a >70MB .TIF image with 10000*10000 pixel, after applying this method, the image was shrinked to ~3MB. It's still 10000*10000 pixel and the quality is still good. That's amazing!
I had a >70MB .TIF image with 10000*10000 pixel, after applying this method, the image was shrinked to ~3MB. It's still 10000*10000 pixel and the quality is still good. That's amazing!
Sunday, August 1, 2010
Use Sigmaplot to export high quality .TIF/.TIFF file
Sigmaplot is a powerful tool for drawing scientific charts, and it is also powerful at exporting high quality .TIF/.TIFF file, which can be directly submitted to the journal editors. Here's what I found out how to get high quality images from Sigmaplot:
Firstly, scale down the original graph. The default size of charts are relatively big, usually ~100mm*100mm. It's better to scale them down to 30%. Then select the items, and use highest DPI possible (for me, 600DPI). And change the exporting size to about 3 -5 time larger than the chart size. This will give you a large and clear TIF image.
Firstly, scale down the original graph. The default size of charts are relatively big, usually ~100mm*100mm. It's better to scale them down to 30%. Then select the items, and use highest DPI possible (for me, 600DPI). And change the exporting size to about 3 -5 time larger than the chart size. This will give you a large and clear TIF image.
Friday, July 30, 2010
AutoCAD TIF output quality Experiments
Since I don't really understand the relationship between DPI, PPI, image size, color mode, and the image quality, I decided to test them out by myself. Although it took me a whole nigh last night, I thought it is worth doing. Now I do know how to get a .TIF image with good quality (no blurry edges of letters and lines) and appropriate image size (several thousand pixels in each dimension, and the file size is about several tens MB).
Here's some tricks what I found out:
1. Scale down the drawing in AutoCAD. This won't decrease the image quality. Actually this can help decrease the file size of the output .TIF file. I used same settings for two prints, one is 10 times scaled up of the other one. And the two files are ~2MB and ~70MB respectively.
2. Use smaller print size in AutoCAD. At first I though I should print a very large size, maybe A2 or A1, to get a good image. Actually this has nothing to do with the image quality. When this size is large, the final .TIF image will be very large. I got a 10,000*10,000 pixel .TIF and my computer got stuck!
3. Usually 600dpi or 720dpi will be good enough.
4. Use 24Bit color. Other color mode doesn't work as well as 24Bit color.
Here's some tricks what I found out:
1. Scale down the drawing in AutoCAD. This won't decrease the image quality. Actually this can help decrease the file size of the output .TIF file. I used same settings for two prints, one is 10 times scaled up of the other one. And the two files are ~2MB and ~70MB respectively.
2. Use smaller print size in AutoCAD. At first I though I should print a very large size, maybe A2 or A1, to get a good image. Actually this has nothing to do with the image quality. When this size is large, the final .TIF image will be very large. I got a 10,000*10,000 pixel .TIF and my computer got stuck!
3. Usually 600dpi or 720dpi will be good enough.
4. Use 24Bit color. Other color mode doesn't work as well as 24Bit color.
Thursday, July 29, 2010
AutoCAD 2009 TIF output poor quality ! Problem solved!
I was drawing a system sketch for one of my journal article in AutoCAD 2009 and was trying to export it to .TIF file because it is required by the journal. However, the .TIF file I got by using TIFOUT command in AutoCAD 2009 was really of low quality-the image is blurry, and the text can not be seen in Windows Picture and Fax Viewer (but can be seen in Adobe Photoshop and Microsoft picture manager, which is very weird).
So I tried many many ways, including use other command, such as PNGOUT, JPGOUT, to make other type of image and tried to convert them into .TIF. But I failed.
Now the reason of the problem is clear: AutoCAD has a crappy driver for .TIF output. And here's the solution: install the free PDFCREATOR! This free software is so handy, that I can set the output file type, size, and resolution. It is not only a PDF creator, but can used to print all kinds of other types.
I loev PDFCREATOR!
So I tried many many ways, including use other command, such as PNGOUT, JPGOUT, to make other type of image and tried to convert them into .TIF. But I failed.
Now the reason of the problem is clear: AutoCAD has a crappy driver for .TIF output. And here's the solution: install the free PDFCREATOR! This free software is so handy, that I can set the output file type, size, and resolution. It is not only a PDF creator, but can used to print all kinds of other types.
I loev PDFCREATOR!
Monday, May 3, 2010
A computer experiment on statistics: confidence interval for mean and variances of normal population
I am reading Statistics for Science and Engineering by Kinney today.In this book, there is a computer experiment assignment like this:
Select 1000 samples, each of size 10, from a N(10,5) distribution. Calculated the mean for each and a 95% confidence interval for u for each sample. Count the number of these confidence intervals that actually contain the true mean, 10.
I used MATLAB to do this.
clc;
clear all;
close all;
% Generate a normally distributed population.
Po=normrnd(10,5,[1,100000]);
SampleSize=10;
SampleNumber=10000;
%plot Po and histogram Po
figure
plot(Po);
figure
hist(Po, 100);
%take samples, each of size 'SampleSize'
Sa=[];
for i=1:SampleNumber
for j=1:SampleSize
index=round(abs(randn(1)*(length(Po)/10-1)))+1;
Sa(i,j)=Po(index);
end
end
%calculate the 95% confidence interval of each sample
mean=[];
interval=[];
for i=1:SampleNumber
mean(i)=sum(Sa(i,:))/SampleSize;
interval(i,1)=mean(i)-(1.96*5/sqrt(SampleSize));
interval(i,2)=mean(i)+(1.96*5/sqrt(SampleSize));
end
%count the samples that contain 10
count=0;
for i=1:SampleNumber
if interval(i,1)>=10 || interval(i,2)<=10
count=count+1;
end
end
count/SampleNumber
The results are around 0.05, which means that those confidence intervals have 5% chance not containing the true mean, 10. That's why those are 95% confidence intervals.
Plot of the population:
Histogram of the population:
Select 1000 samples, each of size 10, from a N(10,5) distribution. Calculated the mean for each and a 95% confidence interval for u for each sample. Count the number of these confidence intervals that actually contain the true mean, 10.
I used MATLAB to do this.
clc;
clear all;
close all;
% Generate a normally distributed population.
Po=normrnd(10,5,[1,100000]);
SampleSize=10;
SampleNumber=10000;
%plot Po and histogram Po
figure
plot(Po);
figure
hist(Po, 100);
%take samples, each of size 'SampleSize'
Sa=[];
for i=1:SampleNumber
for j=1:SampleSize
index=round(abs(randn(1)*(length(Po)/10-1)))+1;
Sa(i,j)=Po(index);
end
end
%calculate the 95% confidence interval of each sample
mean=[];
interval=[];
for i=1:SampleNumber
mean(i)=sum(Sa(i,:))/SampleSize;
interval(i,1)=mean(i)-(1.96*5/sqrt(SampleSize));
interval(i,2)=mean(i)+(1.96*5/sqrt(SampleSize));
end
%count the samples that contain 10
count=0;
for i=1:SampleNumber
if interval(i,1)>=10 || interval(i,2)<=10
count=count+1;
end
end
count/SampleNumber
The results are around 0.05, which means that those confidence intervals have 5% chance not containing the true mean, 10. That's why those are 95% confidence intervals.
Plot of the population:
Histogram of the population:
Thursday, April 29, 2010
Display message in Command Window, not using a pop-up message box
I like to use the input function very much. And I also like to give some instruction to other people who use my code, on what to input, in the command window, but not in a pop-up message box. After a little googling, I found the disp function.
clc;
clear all;
tic;
disp ('Hello, World!');
h=waitbar(0,'Please wait..');
n=0;
for i=1:100
waitbar(i/100)
for j=1:100
for k=0:100;
n=factorial(2);
end
end
end
close(h)
toc
clc;
clear all;
tic;
disp ('Hello, World!');
h=waitbar(0,'Please wait..');
n=0;
for i=1:100
waitbar(i/100)
for j=1:100
for k=0:100;
n=factorial(2);
end
end
end
close(h)
toc
MATLAB progress bar, show the progress of computing
Sometimes during a lengthy procedure, we don't have good way to determine if the code is still running or the computer got stuck. Using a progress bar will let you know approximately how long you have to wait til the run is over. Very cool!
clc;
clear all;
tic;
disp ('Hello, World!');
h=waitbar(0,'Please wait..');
n=0;
for i=1:100
waitbar(i/100)
for j=1:100
for k=0:100;
n=factorial(2);
end
end
end
close(h)
toc
clc;
clear all;
tic;
disp ('Hello, World!');
h=waitbar(0,'Please wait..');
n=0;
for i=1:100
waitbar(i/100)
for j=1:100
for k=0:100;
n=factorial(2);
end
end
end
close(h)
toc
Wednesday, April 28, 2010
xlswrite: output data to excel file {MATLAB functions}
Sometime when a column or a row of data is too long, you can't copy it from MATLAB and then paste directly into an Excel file. A much simpler way to do this is to use the xlswrite function. Just add a line at the end of your code:
xlswrite ('FileName.xls', VariableName)
If you want to put the data into sheet2 in the file, one more input argument is needed:
xlswrite ('FileName.xls', VariableName, 2)
Pretty easy, right?
xlswrite ('FileName.xls', VariableName)
If you want to put the data into sheet2 in the file, one more input argument is needed:
xlswrite ('FileName.xls', VariableName, 2)
Pretty easy, right?
Sunday, April 11, 2010
Set labels as you want
Let's plot something first.
t=-pi:pi/100:pi;
x=sin(t);
plot(t,x)
And here is what the plot looks like:
Then I want to show something different.
set(gca,'xticklabel',{'Jan', 'Feb','Mar', 'and','what', 'ever','you', 'want', 'it', 'to', 'be','!','!@#$@#$'})
t=-pi:pi/100:pi;
x=sin(t);
plot(t,x)
And here is what the plot looks like:
Then I want to set the range of x labels from -4 to 12.
xlim([-4 12])
Then I want to show the positive labels only.
set(gca,'xtick',0:1:12)
set(gca,'xticklabel',{'Jan', 'Feb','Mar', 'and','what', 'ever','you', 'want', 'it', 'to', 'be','!','!@#$@#$'})
Done!
Tuesday, March 16, 2010
Break/Stop a running matlab process: Ctrl + C
I used this to break a dead-end loop long time ago and then forgot about it. It's good a trick to remember.
Friday, February 26, 2010
Some good blog posts
I ran into a blog yesterday, which is a very good source for Matlab fans. And i found some posts are really useful.
This one tells you how to better manage the memory:
http://obasic.net/preallocate-before-you-loop-it-in-matlab
This one shows how to conveniently set up the interface and default settings by using startup file.
http://obasic.net/set-your-customized-startup-file-for-matlab
Though some of them are geeky:
Twitter from Matlab?
http://obasic.net/how-to-twitter-from-matlab
Sending email using Matlab?
http://obasic.net/how-to-send-e-mail-from-matlab
Anyway, very nice job.
This one tells you how to better manage the memory:
http://obasic.net/preallocate-before-you-loop-it-in-matlab
This one shows how to conveniently set up the interface and default settings by using startup file.
http://obasic.net/set-your-customized-startup-file-for-matlab
Though some of them are geeky:
Twitter from Matlab?
http://obasic.net/how-to-twitter-from-matlab
Sending email using Matlab?
http://obasic.net/how-to-send-e-mail-from-matlab
Anyway, very nice job.
Saturday, February 20, 2010
how to calculate payments on a loan using MS Excel?
Excel has lots of useful built-in functions, and PMT is one of them that can calculate payments on a loan for you. Here is a simple example:
And here is how to do the calculation:
Input following into any cell of a Excel worksheet:
The value shown in the cell ($156.68 ) is the money I am going to pay to the bank each month. Here 0.08/12 is used because the 8% is annual rate, so divide it by 12 to get the monthly rate. And in 3 years, there are 3*12 months, which means the number of payment is 36.
Then, what is the total I pay to the bank? As you might have guessed, it is $156.68*36 (=$5640.55). So the bank will earn $640.55 in this deal at the end. Well, that is a lot of money. Now I am thinking about how to own a bank...
P.S: If you didn't buy MS Excel, that's OK! The Google Docs spreadsheet will do the same thing for you! It is right here: docs.google.com
P.S: this has nothing to do with Matlab, but it is still interesting (at least to me) and useful.
If I get a loan of $5000 from a bank, with the annual interest rate of 8%, and I am supposed to pay it off in 3 years, how much should I pay (OR they will ask me to pay) each month?
Input following into any cell of a Excel worksheet:
=PMT(0.08/12, 3*12, 5000)
and press Enter.The value shown in the cell ($156.68 ) is the money I am going to pay to the bank each month. Here 0.08/12 is used because the 8% is annual rate, so divide it by 12 to get the monthly rate. And in 3 years, there are 3*12 months, which means the number of payment is 36.
Then, what is the total I pay to the bank? As you might have guessed, it is $156.68*36 (=$5640.55). So the bank will earn $640.55 in this deal at the end. Well, that is a lot of money. Now I am thinking about how to own a bank...
P.S: If you didn't buy MS Excel, that's OK! The Google Docs spreadsheet will do the same thing for you! It is right here: docs.google.com
P.S: this has nothing to do with Matlab, but it is still interesting (at least to me) and useful.
Linear least-square regression
Of course, the linear least-square regression is an easy thing to do in Excel. Just adding a linear trend-line to the plot will do that for you. Anyway, I wrote this piece of code to do it in Matlab.
% Single least-square linear regression
%Created on 02/17
x=input ('x='); %use [] to input a row
y=input ('y='); %use [] to input a row
X=mean(x);
Y=mean(y);
beta=(sum((x-X).*(y-Y)))/sum((x-X).^2);
alpa=Y-beta*X;
Equation=strcat('y=',num2str(beta), 'x+',num2str(alpa))
The num2str function was used to convert the beta and alpa, which are numbers, into strings.
The strcat function was used to concatenate the strings.
% Single least-square linear regression
%Created on 02/17
x=input ('x='); %use [] to input a row
y=input ('y='); %use [] to input a row
X=mean(x);
Y=mean(y);
beta=(sum((x-X).*(y-Y)))/sum((x-X).^2);
alpa=Y-beta*X;
Equation=strcat('y=',num2str(beta), 'x+',num2str(alpa))
The num2str function was used to convert the beta and alpa, which are numbers, into strings.
The strcat function was used to concatenate the strings.
Monday, February 15, 2010
Give curves random color, just for fun
A few days ago, I drew some curves on a same plot. And now I want to randomize the curve color.
Because the color syntax can be expressed as letters such as k, g, b etc. and also as a three element row, which indicate the RBG mode, such as [1, 0, 1], [0.1,0.7, 0.4] and so on.
So here I wrote a M-file to randomize the color and plot 10 curves in each subplot.
%this code produce a three element row
%which can be used to specify a random color
%created on 02/15/2010
function rndclr=f()
rndclr=[rand, rand, rand];
%this code draws some curves
%of random color
clf
t=0:0.01:100*pi;
for j=1:9
for i=1:10
hold all;
x=(i+2)*cos(t/i)+cos((i-1)*t);
y=i*sin(t/i)+sin((i-3)*t);
subplot (3,3,j);
plot(x,y,'color',rndclr)
axis('equal')
axis off
end
end
And here is the figure:
Because the color syntax can be expressed as letters such as k, g, b etc. and also as a three element row, which indicate the RBG mode, such as [1, 0, 1], [0.1,0.7, 0.4] and so on.
So here I wrote a M-file to randomize the color and plot 10 curves in each subplot.
%this code produce a three element row
%which can be used to specify a random color
%created on 02/15/2010
function rndclr=f()
rndclr=[rand, rand, rand];
%this code draws some curves
%of random color
clf
t=0:0.01:100*pi;
for j=1:9
for i=1:10
hold all;
x=(i+2)*cos(t/i)+cos((i-1)*t);
y=i*sin(t/i)+sin((i-3)*t);
subplot (3,3,j);
plot(x,y,'color',rndclr)
axis('equal')
axis off
end
end
And here is the figure:
M-file, single-variable function integration and boundary on fixed interval
%This is a simple f(x) function
function y =bumpy(x)
%this function draws a bumpy curve
y=1/(4*pi)*(1./((x-2).^2+1)+1./((x+0.5).^4+32)+1./((x+1).^2+2));
%This is how to integrate the function on (-3,3), with tolerance of 10^(-6)
quad('bumpy', -3, 3, 10^(-6))
%This is how to find the minimum boundary of x and y, on (-1.2, 1)
xmin=fminbnd('bumpy',-1.2, 1)
ymin=bumpy(xmin)
%How to find the maximun boundary?
%find minimum boundary for y=-f(x)
%This is example is from Introduction to Matlab with numerical preliminaries by Alexander Stanoyevitch.
function y =bumpy(x)
%this function draws a bumpy curve
y=1/(4*pi)*(1./((x-2).^2+1)+1./((x+0.5).^4+32)+1./((x+1).^2+2));
%This is how to integrate the function on (-3,3), with tolerance of 10^(-6)
quad('bumpy', -3, 3, 10^(-6))
%This is how to find the minimum boundary of x and y, on (-1.2, 1)
xmin=fminbnd('bumpy',-1.2, 1)
ymin=bumpy(xmin)
%How to find the maximun boundary?
%find minimum boundary for y=-f(x)
%This is example is from Introduction to Matlab with numerical preliminaries by Alexander Stanoyevitch.
How do I know if the file name already exist or not?
I tried to save a M-file and was told the file name already exist. How do i know that before I start saving it?
This function would help:
exist ('file_name_you_want_to_check')
Possible output are 0,1,2,3,4,5,6,7,8. However, only 0 means that the file name does not exist and is available to you.
This function would help:
exist ('file_name_you_want_to_check')
Possible output are 0,1,2,3,4,5,6,7,8. However, only 0 means that the file name does not exist and is available to you.
Factorial in Matlab
To calculate the factorial of n in Matlab, that's simple:
factorial (n)
will do it.
Sunday, February 14, 2010
plotting some curves, just for fun
%this code draws some curves
clf
t=0:0.01:100*pi;
for i=1:10
hold all;
x=(i+2)*cos(t/i)+cos((i-1)*t);
y=i*sin(t/i)+sin((i-3)*t);
plot (x,y,'red')
end
axis('equal')
clf
t=0:0.01:100*pi;
for i=1:10
hold all;
x=(i+2)*cos(t/i)+cos((i-1)*t);
y=i*sin(t/i)+sin((i-3)*t);
plot (x,y,'red')
end
axis('equal')
Find the element you want in matrix
a =
1 2 3
1 3 4
1 4 5
2 5 6
2 6 7
3 7 8
2 8 9
1 0 1
2 1 2
the command:
b = find ( a ( : , 1 )= =2)
returns following results:
b =
4
5
7
9
the coomand:
c= a ( b , : )
returns following results:
c =
2 5 6
2 6 7
2 8 9
2 1 2
This is how to pick out all the rows in which the first element is 2 in matrix a and put them into c. I could have used this to handle my ozone historic data and it will be much easier.
1 2 3
1 3 4
1 4 5
2 5 6
2 6 7
3 7 8
2 8 9
1 0 1
2 1 2
the command:
b = find ( a ( : , 1 )= =2)
returns following results:
b =
4
5
7
9
the coomand:
c= a ( b , : )
returns following results:
c =
2 5 6
2 6 7
2 8 9
2 1 2
This is how to pick out all the rows in which the first element is 2 in matrix a and put them into c. I could have used this to handle my ozone historic data and it will be much easier.
Sunday, February 7, 2010
Use stopwatch to measure the time elapsed
run 3D random walk for 30 times and get the average time of each run.
% random walk
for k=1:30
tic;
x=[];
y=[];
z=[];
x(1)=0;
y(1)=0;
z(1)=0;
tTotal=0;
for i=1:1000
J=rand*6;
if J<1
x(i+1)=x(i)+1;
y(i+1)=y(i);
z(i+1)=z(i);
elseif (J>1)&(J<2)
x(i+1)=x(i)-1;
y(i+1)=y(i);
z(i+1)=z(i);
elseif (J>2)&(J<3)
x(i+1)=x(i);
y(i+1)=y(i)+1;
z(i+1)=z(i);
elseif (J>3)&(J<4)
x(i+1)=x(i);
y(i+1)=y(i)-1;
z(i+1)=z(i);
elseif (J>4)&(J<5)
x(i+1)=x(i);
y(i+1)=y(i);
z(i+1)=z(i)+1;
else
x(i+1)=x(i);
y(i+1)=y(i);
z(i+1)=z(i)-1;
end
end
plot3(x,y,z)
t(i)=toc;
tTotal=tTotal+t(i);
end
tAve=tTotal/10;
when i=1:2000, tAve=0.0032s
when i=1:4000, tAve=0.0114s
when i=1:6000, tAve=0.0506s
when i=1:12000, tAve=0.1882s
when i=1:24000, tAve=0.8823s.
when i=1:48000, tAve=2.7501s.
% random walk
for k=1:30
tic;
x=[];
y=[];
z=[];
x(1)=0;
y(1)=0;
z(1)=0;
tTotal=0;
for i=1:1000
J=rand*6;
if J<1
x(i+1)=x(i)+1;
y(i+1)=y(i);
z(i+1)=z(i);
elseif (J>1)&(J<2)
x(i+1)=x(i)-1;
y(i+1)=y(i);
z(i+1)=z(i);
elseif (J>2)&(J<3)
x(i+1)=x(i);
y(i+1)=y(i)+1;
z(i+1)=z(i);
elseif (J>3)&(J<4)
x(i+1)=x(i);
y(i+1)=y(i)-1;
z(i+1)=z(i);
elseif (J>4)&(J<5)
x(i+1)=x(i);
y(i+1)=y(i);
z(i+1)=z(i)+1;
else
x(i+1)=x(i);
y(i+1)=y(i);
z(i+1)=z(i)-1;
end
end
plot3(x,y,z)
t(i)=toc;
tTotal=tTotal+t(i);
end
tAve=tTotal/10;
Resutls:
when i=1:1000, tAve=0.0012swhen i=1:2000, tAve=0.0032s
when i=1:4000, tAve=0.0114s
when i=1:6000, tAve=0.0506s
when i=1:12000, tAve=0.1882s
when i=1:24000, tAve=0.8823s.
when i=1:48000, tAve=2.7501s.
Friday, February 5, 2010
3D Random walk
2D Random walk program
Why I want to do this?
http://en.wikipedia.org/wiki/Random_walk
Here's the code I wrote:
% random walk
x=[];
y=[];
x(1)=0;
y(1)=0;
for i=1:100000
J=rand;
if J<0.25
x(i+1)=x(i)+1;
y(i+1)=y(i);
elseif (J>0.25)&(J<0.5)
x(i+1)=x(i)-1;
y(i+1)=y(i);
elseif (J>0.5)&(J<0.75)
x(i+1)=x(i);
y(i+1)=y(i)+1;
else
x(i+1)=x(i);
y(i+1)=y(i)-1;
end
end
plot(x,y)
And here are the figure I got:
http://en.wikipedia.org/wiki/Random_walk
Here's the code I wrote:
% random walk
x=[];
y=[];
x(1)=0;
y(1)=0;
for i=1:100000
J=rand;
if J<0.25
x(i+1)=x(i)+1;
y(i+1)=y(i);
elseif (J>0.25)&(J<0.5)
x(i+1)=x(i)-1;
y(i+1)=y(i);
elseif (J>0.5)&(J<0.75)
x(i+1)=x(i);
y(i+1)=y(i)+1;
else
x(i+1)=x(i);
y(i+1)=y(i)-1;
end
end
plot(x,y)
And here are the figure I got:
Subscribe to:
Posts (Atom)
my-alpine and docker-compose.yml
``` version: '1' services: man: build: . image: my-alpine:latest ``` Dockerfile: ``` FROM alpine:latest ENV PYTH...
-
It took me a while to figure out how to insert a space in Mathtype equations. This is especially useful when you write an equation with mult...
-
Recently I read post from Dr. Doug Hull's blog: http://blogs.mathworks.com/videos/2009/10/23/basics-volume-visualization-19-defining-s...
-
To get the slope of a pair of x and y, usually I first plot the curve and then add the trend line. Actually there are two functions i...