new to matlab. trying to plot simple line and not successful from a script file!

r=0.0;
z=0.1;
h=0.1;
for i=1:50
p=sqrt((1+(2*z*(r))^2)/((1+(r)^2)+(2*z*(r))))
r=r+h
end
plot(p,r)
title('tr function')
There is just a blank figure screen when i run this. help please!

1 件のコメント

Abdullah Caliskan
Abdullah Caliskan 2014 年 1 月 18 日
編集済み: Walter Roberson 2014 年 1 月 18 日
r=0:1:50;
z=0.1; h=0.1;
p=sqrt((1+(2.*z.*(r)).^2)./((1+(r).^2)+(2.*z.*(r))));
plot(p,r); title('tr function');
try this one

サインインしてコメントする。

 採用された回答

Amit
Amit 2014 年 1 月 19 日
編集済み: Amit 2014 年 1 月 19 日
Here you go:
h = 0.1;
r = 0:0.1:0.1*50;
z = 0.1:0.1:1;
fileID = fopen('my_p_data.txt', 'wt');
rr = {'b','r','k','g','-','--',':','-.','-k','--k'};
legstr = {};
for i = 1:numel(z)
p = sqrt((1+(2*z(i)*(r)).^2)./((1+(r).^2)+(2*z(i)*(r))));
fprintf(fileID,'%f\n', p);
plot(p,r,rr{i});
legstr{i}= ['Z = ' num2str(z(i))];
hold on;
end
fclose(fileID);
title('tr function')
legend(legstr);

その他の回答 (6 件)

Amit
Amit 2014 年 1 月 18 日
編集済み: Amit 2014 年 1 月 18 日
This might work.
h = 0.1;
r = 0:0.1:0.1*50;
z = 0.1;
p = sqrt((1+(2*z*(r)).^2)./((1+(r).^2)+(2*z*(r))));
plot(p,r)
title('tr function')
The reason it was not plotting anything cause once you get out of the loop, you had a single value for p and r, and you cannot plot a line using a single value!
Praketa
Praketa 2014 年 1 月 18 日

0 投票

ok it worked but i wanted to know how we can use the fid() function, store data in txt file and reload to output while utilizing the loops. i have a c++ background so it is easier for me to follow that logic. the matrix operations are confusing for now.
and i also want to vary the z variable and from 0.1 to 1. if i use this then I am getting unmatched dimensions error.
i saw on some forum to load this data into txt file...does anybody know about that!

4 件のコメント

Amit
Amit 2014 年 1 月 18 日
I am lost? what do you mean by 'reload to output'?
Praketa
Praketa 2014 年 1 月 18 日
fine lets work with this right now...what if i want to vary z as well but from 0.1 to 1, apart from r. wont i have a dimensions mismatch. actually i tried and it did
Amit
Amit 2014 年 1 月 18 日
編集済み: Amit 2014 年 1 月 18 日
h = 0.1;
r = 0:0.1:0.1*50;
z = 0.1:0.1:1;
fileID = fopen('my_p_data.txt', 'wt');
for i = 1:numel(z)
p = sqrt((1+(2*z(i)*(r)).^2)./((1+(r).^2)+(2*z(i)*(r))));
fprintf(fileID,'%f\n', p);
plot(p,r)
hold on;
end
fclose(fileID);
title('tr function')
In every loop, the new value of z (0.1,0.2 ... 1) is used.
and you can use Image Analyst way to store p generated in every loop.
Amit
Amit 2014 年 1 月 19 日
h = 0.1;
r = 0:0.1:0.1*50;
z = 0.1:0.1:1;
fileID = fopen('my_p_data.txt', 'wt');
for i = 1:numel(z)
p = sqrt((1+(2*z(i)*(r)).^2)./((1+(r).^2)+(2*z(i)*(r))));
fprintf(fileID,'%f\n', p);
figure;
plot(p,r)
end
fclose(fileID);
title('tr function')

サインインしてコメントする。

Image Analyst
Image Analyst 2014 年 1 月 18 日
Did you look at the documentation for fprintf. Here's an example:
x = 0:.1:1;
A = [x; exp(x)];
fileID = fopen('exp.txt','w');
fprintf(fileID,'%6s %12s\n','x','exp(x)');
fprintf(fileID,'%6.2f %12.8f\n',A);
fclose(fileID);
For your case
fileID = fopen('my_p_data.txt', 'wt');
fprintf(fileID,'%f\n', p); % Write p out to a file.
fclose(fileID);
Praketa
Praketa 2014 年 1 月 19 日

0 投票

thank you much everybody for the help so far. but i wanted different graphs showing influence of z and the above code only outputs one graph!

1 件のコメント

Amit
Amit 2014 年 1 月 19 日
I have edited the code to give you different plots with changing z

サインインしてコメントする。

Praketa
Praketa 2014 年 1 月 19 日

0 投票

Amit loads of thanks. but almost. can i have these figures all in in single figure. i want my students to see the relation. anyhow i can recommend ur skills I would be glad to.

1 件のコメント

Praketa
Praketa 2014 年 1 月 19 日
any different graphs i meant all the graphs with different z values but in the same figure

サインインしてコメントする。

Praketa
Praketa 2014 年 1 月 19 日

0 投票

u r awesome amit! thanks a ton!

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

製品

質問済み:

2014 年 1 月 18 日

回答済み:

2014 年 1 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by