How to save values in a specific position+stop output

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.

Answers (1)

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 Comments

The attached documents are my code and my function (sorry for the swedish language comments). If i use what you commented above, i get a 1x11 double, but the subsequent plot only goes from P=5 to P=11. I still need the volym-function to be plotted from P=5 to P=15
Thanks! Replacing helped with getting unneccessary outputs! :)
now i still have trouble with my volym output being a 1x15 double and not a 1x10 double.
Just to clarify, i want Volym to only include values of fzero for P=5 to P=15.
How do i fix this?
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.
If it’s what you were looking for make sure to accept the answer else let know.

Sign in to comment.

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Edited:

on 7 Jan 2019

Community Treasure Hunt

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

Start Hunting!