Simple subtraction from column/row - HELP

Hello, let me first introduce myself. I am a 32 (nearly 33) year old mature student in my final year of engineering study.
Secondly, I am a complete NOVICE when it comes to matlab but have chosen this module during my final year of study as I believe matlab will benefit me more in the future as opposed to CFD (for example) or stress analysis via a CAD package!
Anyway, I have an issue which I can't seem to iron out...
Brief description of what I am trying to do;
Am compiling a matrix [m,n] (in my current example, is [4, 7]) and need to subtract a constant from each column in a linear manner.
Using the 3x5 as an example below if subtracting 5 from 50, the desired result is;
50 45 40 35 30
50 0 0 0 0
50 45 40 35 30
Instead I'm getting;
50 45 45 45 45
50 0 0 0 0
50 45 45 45 45
My for loop takes the shape;
for i=[1:a-1]
C(1,i+1)=T-td;
C(b,i+1)=T-td;
end
Where T = 50 and td = 8.333 (not that I imagine they are relevant to you mega minds).
So what I need is to know where I am going wrong (assuming the T-td is at fault somehow) and best way to fix it please?
I will just add that this is part of an assignment I have on but by you providing me with assistance, you are not aiding a cheat. I am perfectly compliant within rules and regs for seeking knowledge from more experienced and reliable sources. Getting to see my lecturer can be tricky (lecture times/ school pick-ups etc) so just trying to save time by coming on here.
I have looked on several pages here and on 'help' at the campus but I'm as much use as a chocolate fireguard sometimes and now is one of them times... Can't find what I need (or it's just not making sense).
I am not totally sure what version MatLab is on the campus systems either (chocolate fireguard?), think they appear to vary from stacks that were connected to the network but no longer are and stacks that are connected to a network so may range from 2010-2014 (possibly).
I thank you in advance for any help you may provide. Also, would you offer a brief description to better my understanding (really struggle with for loops, just like I have with differentiation for years - enjoy basic matrices and integration though (strange boy!)).

6 Kommentare

I’m lost.
First, it will be easier to help you if we know what you’re starting with and what you want your code to output.
Second, what are ‘a’ and ‘b’ here:
for i=[1:a-1]
C(1,i+1)=T-td;
C(b,i+1)=T-td;
end
and how do they relate to what you want to do?
Adam
Adam am 17 Nov. 2014
Why do you have 0s on the 2nd row in your desired output if you are trying to subtract a constant from each column where your 1st and 3rd rows suggest you are subtracting 5 each column further across?
Leighton
Leighton am 17 Nov. 2014
Bearbeitet: Leighton am 17 Nov. 2014
Ok, basically, I am tasked with calculating the temperature distribution through a 2D plate (3D after xmas), left to right, using the finite difference method (FDM). For this, an initial boundary is required and have been told to consider the outer conditions as a linear decrease, value dependent on no. of columns, i.e. T~50, [a=4,b=7], linear decrease = 8.333 per column resulting in a T~0 at end.
The 0's I suppose are another matrix of zeros who's values will depend on a number of other 'input' variables such as conductivity, density, specific heat, x, y, grid size etc and will eventually take the form Tt=(Te+Tw+Ts+Tn-(4-(1/Fo))*Tt)*Fo where Fo is Fouriers value.
I hope this paints a clearer picture? But yes, I am in need of leaving many values at zero until further in the code (i.e. T(2,2), T(2,3), T(3,2), T(3,3) et al. ~=0)
It's just getting the columns to deduct in the fashion I need that has got me scratching my head (for now...)!
50.0000 41.6667 41.6667 41.6667 41.6667 41.6667 41.6667
50.0000 0 0 0 0 0 0
50.0000 0 0 0 0 0 0
50.0000 41.6667 41.6667 41.6667 41.6667 41.6667 41.6667
That's exactly as I see it with the for loop above included. I need Col. 3 to be 33.333, col. 4 to be 25.### etc
Again, thanks in advance.
Star Strider
Star Strider am 17 Nov. 2014
@Adam — I don’t believe that’s his desired output. It’s what his code is producing.
Leighton
Leighton am 17 Nov. 2014
It's almost my desired output but I only need to subtract from first and last rows (leaving 0 in all other rows/columns as displayed).
I've looked at my previous LU decomposition, taylor series and gaussian codes but can't find anything of help!!
Leighton
Leighton am 18 Nov. 2014
Bearbeitet: Leighton am 18 Nov. 2014
Nevermind, I've sussed it.
For anyone interested, i found this sorted it;
for i=[1:a-1]
for j=i+1;
C(1,j)=C(1,i)-td;
C(end,j)=C(end,i)-td;
end
end
Just needs an if statement to deal with negligible but potential negative decimals.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

MA
MA am 18 Nov. 2014

0 Stimmen

clear all
close all
clc;
A=[50;50;50];
a=5;
for i=1:a-1
A(1,i+1)=A(1,i)-a;A(2,i+1)=0;A(3,i+1)=A(1,i)-a;
end
A

Gefragt:

am 17 Nov. 2014

Kommentiert:

am 18 Nov. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by