user172675
user172675

Reputation: 69

MatLab matrix construction

If I construct the matrix A1 as follow:

a1=2; 
v1i=0;
t1i=0; 
dt=0.1; 
syms t real 
t1f=solve(int(a1,t)+v1i==40,t); 
t1=t1i:dt:t1f;
A1(:,1)=t1;
A1(:,2)=a1;

then A1 is displayed as:

[      0, 2]
[   1/10, 2]
[    1/5, 2]
[   3/10, 2]
...

However, I want A1 to be like this:

   0    2.0000
0.1000    2.0000
0.2000    2.0000
0.3000    2.0000
...

After a bit playing with this, I noticed that t1f and perhaps how it is calculated effects the shape of the matrix. So

a1=2;  
v1i=0;
t1i=0; 
dt=0.1; 
t1=t1i:dt:20;
A1(:,1)=t1;
A1(:,2)=a1;

produces the desired result. How can I get my desired matrix shape without removing the calculation for t1f?

Upvotes: 0

Views: 53

Answers (2)

TroyHaskin
TroyHaskin

Reputation: 8401

As an alternative to the double solution, you can perform the solution numerically and avoid the Symbolic Toolbox altogether:

t1f = fzero(@(t) integral(@(t)a1,0,t,'ArrayValued',true)+v1i-40,v1i); 

Upvotes: 1

user172675
user172675

Reputation: 69

I think that I found an answer. t1f is a sym so if I make it double, then I get my desired result:

a1=2; 
v1i=0;
t1i=0; 
dt=0.1; 
syms t real 
t1f=double(solve(int(a1,t)+v1i==40,t)); 
t1=t1i:dt:t1f;
A1(:,1)=t1;
A1(:,2)=a1;

Upvotes: 2

Related Questions