matlab coding question help~

12 Ansichten (letzte 30 Tage)
ONION LIM
ONION LIM am 8 Okt. 2013
Kommentiert: Les Beckham am 1 Dez. 2022
hallo, i got some question here. my question is i need to read some data from a notepad file then use the data to continue the equation... example, i created a notepad, inside the notepad is
1 2
3 4
5 6
then i use
T=importdata('trial.txt')
U1=T(1,1)
U2=T(1,2)
U3=T(2,1)
U4=T(2,2)
U5=T(3,1)
U6=T(3,2)
then put inside an equation
V1=U1+1
V2=U2+1
V3=U3+1
V4=U4+1
V5=U5+1
V6=U6+1
is there any shorter code can use??? this seem like very long... and when the number of data in the notepad change then everything cant use, any idea?
  3 Kommentare
Krishanth
Krishanth am 1 Dez. 2022
If a second order polynomial is written in the general form:
then the roots (i.e. the values of x that satisfy the equation) can be determined using the quadratic formula:
Code has already been provided to define a function named quadraticSolver that accepts a 3-element row vector and assigns it to the input variable abc, with values for a, b, and c in the first, second, and third column respectively. Add commands to do the following:
1. Check if the roots of the polynomial defined by a, b, and c are real and distinct. Note that the roots will be real if the value under the square root is greater than or equal to zero. They will not be distinct, however, if the value under the square root is exactly equal to zero. Assign an an integer (uint8 datatype) value to the output variable rootCondition as follows:
  • The value 2 if the roots are real and distinct
  • The value 1 if the two roots are the same.
  • The value 0 if the roots are not real (i.e. complex because the value under the square root is less than 0)
2. Assign the solution to the quadratic to the output variable rootValues according to the following guidelines:
  • In cases where there are two real and distinct roots, compute the two roots and assign them in ascending order to a two-element row vector.
  • In cases where the two roots are the same, compute the root and assign this value to both elements in a two-element row vector.
  • In cases where the roots are complex, output assign the text 'complex roots' output variable rootValues.
Note the value of abc is defined as a function input. Do not overwrite this value in your code. Be sure to assign values to each of the function output variables.
Use an if-elseif-else structure in your code.
Les Beckham
Les Beckham am 1 Dez. 2022
Why did you post your homework assignment as a comment responding to a completely unrelated 9 year old question?
If you have a specific Matlab related question, post it as a new question and show what you have tried so far (your Matlab code), and what isn't working as you expect.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Cedric
Cedric am 8 Okt. 2013
Bearbeitet: Cedric am 8 Okt. 2013
Convert T into a vector with correct dimension and ordering, and then work in a vector way (I removed the ';' at the end of each line so you can see the output):
>> T = reshape(T.', [], 1)
T =
1
2
3
4
5
6
>> V = T + 1
V =
2
3
4
5
6
7
  2 Kommentare
ONION LIM
ONION LIM am 8 Okt. 2013
if say my equation is not V = T + 1, but is like
V1=U1+U2
V2=U2+U3
V3=U3+U4
V4=U4+U5
V5=U5+U6
V6=U6+U1
then how should i write it??
Cedric
Cedric am 8 Okt. 2013
Bearbeitet: Cedric am 8 Okt. 2013
Several possibilities, one is simply
V = T + [T(2:end); T(1)] ;
where the 2:end range of indices works whatever the length of T (which is not hardcoded). More generally, the tools that you have at hand are: indexing, array concatenation [], and functions for array manipulation like RESHAPE, FLIPLR, FLIPUD, VERTCAT, HORZCAT, CIRCSHIFT, etc.

Melden Sie sich an, um zu kommentieren.

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by