new to matlab. trying to plot simple line and not successful from a script file!
Show older comments
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 Comment
Abdullah Caliskan
on 18 Jan 2014
Edited: Walter Roberson
on 18 Jan 2014
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
Accepted Answer
More Answers (6)
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
on 18 Jan 2014
0 votes
4 Comments
Amit
on 18 Jan 2014
I am lost? what do you mean by 'reload to output'?
Praketa
on 18 Jan 2014
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
on 19 Jan 2014
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
on 18 Jan 2014
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
on 19 Jan 2014
0 votes
Categories
Find more on Graphics Objects in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!