adding a variable into a vector

2 views (last 30 days)
Ellie Matlab
Ellie Matlab on 28 Jun 2022
Commented: Ellie Matlab on 28 Jun 2022
I need to add extra variable to my matrix b, but when i added and ran the codes, there is an error as it does not fulfil the requirements. From what i understand, i have already declared the gridpoints, L, w and k.
N = 3;
a1 =-2;
a2 = 1;
a3 = 1;
w=200000;
k=80.2;
deltax= 0.8/5-1;
A = diag(a1*ones(1,N)) + diag(a2*ones(1,N-1),1) + diag(a3*ones(1,N-1),-1);
b = zeros(3,1); b(1,1) = -40-(w/k)*deltax^2; b(3,1) = -20-(w/k)*deltax^2; b(2,1)=-(w/k)*deltax^2;
T=A\b;
x=linspace(0,0.8,5);
Temperature = ones(1,5);
Temperature(1,1)=40;
Temperature(1,5)=20;
Temperature(2:end-1)=T;
plot(x,Temperature)
title('Temperature distribution between x=0 and x=0.8 ')
xlabel('x')
ylabel('Temperature')

Answers (1)

Dyuman Joshi
Dyuman Joshi on 28 Jun 2022
Your deltax is incorrectly defined and due it all subsequent calculations are incorrect as well.
%It should be
L=0.8;
gridpoints=5;
deltax = L/(gridpoints-1) %=0.8/(5-1)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!