简体   繁体   中英

How to choose a specific value from a loop to be plotted against a range of values?

I cannot think of the proper wording for this, but I am trying to have my code run a loop that will input a value of X into an initial condition variable. This variable is then inputted into the heat equation to be plotted. From the code I want to choose a value which is at X(i=51) and plot it as T(x,T1). As i said before I don't know the proper wording to search for a possible solution. Any advice would be great!

clear;
clc;
% initialize given variables
A= 0.25;
L= pi;
Nx=101; Nt=10^(-4);
dx=L/(Nx-1);

T1=zeros(1,Nx);
x=linspace(0, L, Nx); %x distance

%Initial condition
%T1 will be the "new" T value and To will be the old
T1= x.*(pi-x);
%For plotting, time starts at 0.
t=0;
for n=1:50
    To=T1;
    t=t+1;
    for i=2:Nx-1
        T1(i)=To(i)+Nt *A*((To(i+1)-2*To(i)+To(i-1))/(dx^2))+ sin(5*x(i));
    end
    %B.C given than @ T(0,t) & T(L,t) = 0
    T1(1)=0; T1(end)=0;
    figure(1)
    plot(x,T1); set(gca, 'ylim',[-110 110]);
    ylabel('Temperature along the Rod');
    xlabel('Location on the Rod of length Pi');
    title(sprintf('Time = %f seconds', t));
    pause(0.001);
end

The expected out put that I want to plot is plot(x(i=51),T1) which would show an image just like this. For this plot I ran my code and altered i to =50:51 to get the needed values for the heat equation. I am trying to have this be plotted in the code shown and not have to rewrite my code over and over to get different plots because I change values such as i or time ect... 在此处输入图片说明

You want to plot a consistent value of x , specifically x(51) , for every n .

This can be done a couple of ways

First, as individual points with hold on :

for n = 1:50
    % ... calculation ...
    hold on
    plot( x(51), T1(51), '.' );
    hold off
end

You could update the plot, this will be quicker to compute and has the advantage of showing a line plot

x_51 = NaN( 50, 1 );
T1_51 = NaN( 50, 1 );
p = plot( [], [] );
for n = 1:50
    % ... calculation ... 
    x_51(n) = x(51);
    T1_51(n) = T1(51);
    set( p, 'XData', x_51, 'YData', T1_51 );
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM