38 views (last 30 days)

I have created a code to solve a differential in terms of one variable (all C variables and B are constants and defined previously). Now I want to plug a value in for that variable into the differential equation, but it won't let me. I tried using the 'subs' function, but that doesn't work. Help?

syms I1 I2

F1 = C1*(exp(C2*(I1-3)-1))-((C1*C2)/2)*(I2-3);

W1 = 0;

F2 = piecewise(x<1,W1,1<x<lambda,W2,x>lambda,W3);

Wo = F1 + F2;

Wo1 = diff(Wo,I1);

Wo2 = diff(Wo,I2);

clear I1 I2

I1 = trace(B);

I2 = .5*((trace(B))^2+trace(B^2));

subs(Wo1,I1,I2);

Karan Gill
on 21 Feb 2018

You shouldn't overwrite I1. Overwriting I1 will not automatically substitute for it in Wo1. Instead, remove I1 = trace(B); and use

>> I1_val = trace(B)

I1_val =

2/x + x^2

>> Wo1 = subs(Wo1,I1,I1_val)

Wo1 =

[ piecewise(x < 1 | 139/50 < x | x in Dom::Interval(1, 139/50), (3317*exp((107*I1)/100 - 421/100))/2500000), piecewise(x < 1 | 28/5 < x | x in Dom::Interval(1, 28/5), (3317*exp((107*I1)/100 - 421/100))/2500000), piecewise(x < 1 | 387/50 < x | x in Dom::Interval(1, 387/50), (3317*exp((107*I1)/100 - 421/100))/2500000)]

This way, you separate the symbolic variable from the value you want to substitute, which keeps the meaning of variables in your code clean.

Karan Gill
on 21 Feb 2018

Great! :) (And I understand overwriting I1 was the intuitive thing to do.)

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/383884-substitute-value-for-variable#comment_537684

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/383884-substitute-value-for-variable#comment_537684

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/383884-substitute-value-for-variable#comment_537757

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/383884-substitute-value-for-variable#comment_537757

Sign in to comment.