How to save values in a specific position+stop output
1 次查看(过去 30 天)
显示 更早的评论
I am using fzero to solve a non-linear function, and then using a for-loop to plot the function for different values of p.
volume=fzero(@(V) volume(V,P), fstart);
for P=5:1:15
Volume=fzero(@(V) volume(V,P),fstart);
end
P=P+1;
plot(Volym)
Several questions;
How do i stop the loop from displaying every value of 'Volume' in the command window? The ';' in the loop doesnt seem to stop this.
How do i get the Volume loop to store the values in a 1x10 double, starting with P=5 on the first and P=15 on the tenth? Currently, the loop saves the first 5 (for P=0 until P=4 as zeros, which makes the Volume expression a 1x15 double instead. So essentially i need a way to ignore the first 5 columns.
0 个评论
回答(1 个)
madhan ravi
2019-1-2
You need to upload your missing parts of your code to be debugged:
volume=fzero(@(V) volume(V,P), fstart);
P=5:15;
Volume=zeros(size(P)); % preallocate
for i=1:numel(P)
Volume(i)=fzero(@(V) volume(V,P(i)),fstart);
end
plot(Volym)
4 个评论
madhan ravi
2019-1-2
编辑:madhan ravi
2019-1-2
P has only 11 points :
n=2;
a=1.360;
b=0.003183;
R=0.0821;
T=300;
P=5;
% fplot(@(V) volym(V,P),[0 0.1]);
fstart=0.1;
P=5:15; % if you want 15 points then try linspace(5,15,15)
Volume=zeros(size(P)); % preallocate
for i=1:numel(P)
Volume(i)=fzero(@(V) volym(V,P(i)),fstart); % function call
end
Volym=volym(Volume,P);
xx=linspace(P(1),P(end),1000);
yy=interp1(P,Volym,xx,'spline'); % for a smooth curve
plot(P,Volym,'o',xx,yy)
% axis([5 15 0.1 0.12])
title('Volume dependent on Pressure')
xlabel('Pressure')
ylabel('Volume')
function Volym=volym(V,P) % function definition
n=2;
a=1.360;
b=0.003183;
R=0.0821;
T=300;
Volym=(P+(a.*(n./V).^2)).*(V-(n.*b))-n.*R.*T;
end
The graph :
Note: I still have no idea what you are trying to do but clarify.
madhan ravi
2019-1-7
编辑:madhan ravi
2019-1-7
If it’s what you were looking for make sure to accept the answer else let know.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!