Effacer les filtres
Effacer les filtres

how to get the plot from function

1 vue (au cours des 30 derniers jours)
Aleksandra Pawlak
Aleksandra Pawlak le 27 Avr 2021
Hi!
I want to get the three plots on one chart: plot(x1,f(x1), x2,f(x2), x3, f(x3))
I have some errors when i call plot(x1,f(x1)).
f is included inside the function:
f = @(x) 1/(1+(exp(10*(3-x))))-(1/(1+(exp(10*(2-x)))));
clc
close all
clear all
N1=7
u=poisson(N1)
dx1 = (6-0)/(N1-1);
x1 = 0:dx1:6;
figure(1)
plot(x1,u)
% hold on
N2=21
u=poisson(N2)
dx2 = (6-0)/(N2-1);
x2 = 0:dx2:6;
plot(x2,u)
hold on
N3=199
u=poisson(N3)
dx3 = (6-0)/(N3-1);
x3 = 0:dx3:6;
plot(x3,u)
legend(['N1=',num2str(N1)], ['N2=',num2str(N2)], ['N3=',num2str(N3)])
xlabel('x')
ylabel('u')
hold off
function u=poisson(Nx)
A = zeros(Nx,Nx);
b = zeros(Nx,1);
f = @(x) 1/(1+(exp(10*(3-x))))-(1/(1+(exp(10*(2-x)))));
% Macierze A i b
dx = (6-0)/(Nx-1);
x = 0:dx:6;
h=dx;
m=1;
if x>=0 & x<2
m=1;
elseif x>=2 & x<4
m=5;
else x>=4 & x<=6
m=10;
end
for i=1:Nx
if i == 1
A(i,i) = 1;
b(i) = 0;
elseif i == Nx
b(i) = 1;
A(i,i) = 1;
else
A(i,i-1) = m/dx^2;
A(i,i) = -m*(2/dx^2);
A(i,i+1) = m/dx^2;
b(i) = f(x(i));
end
end
u = A\b;
end

Réponse acceptée

Star Strider
Star Strider le 27 Avr 2021
Uncomment this hold call —
figure(1)
plot(x1,u)
% hold on
N2=21
u=poisson(N2)
and the code does what you want.
Note that the lines are almost the same, however the legend call demonstrates that they all plot correctly.

Plus de réponses (0)

Catégories

En savoir plus sur Scatter Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by