how to correctly use the function "subs" (2024)

28 views (last 30 days)

Show older comments

Tony Cheng on 24 Jul 2024 at 3:08

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2139761-how-to-correctly-use-the-function-subs

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2139761-how-to-correctly-use-the-function-subs

Commented: Umar on 24 Jul 2024 at 7:39

Accepted Answer: Piyush Kumar

Hi there,

I am using subs to replace some symbols in a symbolic expression Jacobian_mtx_1. The symbols that I want to be replaced are A, B and C, which are a 6*1 symbolic vector, a symbolic scalar, and a 2*1 symbolic vector, respectively. I write the code:

Jacobian_mtx_1 = subs( Jacobian_mtx_1 , { A , B , C } , { A_val ,B_val , C_val } ) ;

where A_val is a 6*1 vector containing 6 function calls, B_val is a numeric scalar, and C_val is a 2*1 numeric vector.

However, the software reminds me that

Errors using sym/subs

Entries in second argument must be scalar.

Could anyone tell me how to resolve this issue?

Many thanks!

3 Comments

Show 1 older commentHide 1 older comment

Umar on 24 Jul 2024 at 3:53

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2139761-how-to-correctly-use-the-function-subs#comment_3219171

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2139761-how-to-correctly-use-the-function-subs#comment_3219171

Hi Tony,

When using the subs function in MATLAB to substitute symbols in a symbolic expression, the replacement values must be scalar. In your case, A_val is a 61 vector, B_val is a numeric scalar, and C_val is a 21 numeric vector, which is causing the error. To resolve this issue, you need to ensure that the replacement values are scalar. One approach is to replace each element of the vector with the corresponding element in the replacement vector. Here's how you can modify your code to achieve this:

% Define symbolic vectors and scalars

syms A B C

A_val = sym('A_val', [6, 1]);

B_val = sym('B_val');

C_val = sym('C_val', [2, 1]);

% Define the symbolic expression Jacobian_mtx_1

Jacobian_mtx_1 = A + B + C; % Example expression

% Perform substitution with scalar values

for i = 1:numel(A)

 Jacobian_mtx_1 = subs(Jacobian_mtx_1, A(i), A_val(i));

end

Jacobian_mtx_1 = subs(Jacobian_mtx_1, B, B_val);

for i = 1:numel(C)

 Jacobian_mtx_1 = subs(Jacobian_mtx_1, C(i), C_val(i));

end

So, in the above modified code snippet, I iterate over each element of the symbolic vectors A and C to perform individual substitutions with the corresponding elements in A_val and C_val, respectively. For the scalar symbol B, a direct substitution is made with B_val. By ensuring that the replacement values are scalar or by individually substituting elements of vectors, you can resolve the error and successfully substitute symbols in your symbolic expression. I hope this answers your question.

Tony Cheng on 24 Jul 2024 at 6:47

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2139761-how-to-correctly-use-the-function-subs#comment_3219246

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2139761-how-to-correctly-use-the-function-subs#comment_3219246

Hi Umar,

Many thanks to your reply! I think your way is feasible, but the following manner is a little bit easier

Jacobian_mtx_1 = subs( Jacobian_mtx_1 , A , A_all ) ;

Jacobian_mtx_1 = subs( Jacobian_mtx_1 , B , B_val ) ;

Jacobian_mtx_1 = subs( Jacobian_mtx_1 , C , C_val) ;

cause on

it says If old and new are both vectors or cell arrays of the same size, subs replaces each element of old with the corresponding element of new.

Then we can use vector replacements directly.

Cheers

Umar on 24 Jul 2024 at 7:39

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2139761-how-to-correctly-use-the-function-subs#comment_3219296

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2139761-how-to-correctly-use-the-function-subs#comment_3219296

No problem, Tony, glad to help you out. Please let us know if you have any further questions.

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Piyush Kumar on 24 Jul 2024 at 4:07

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2139761-how-to-correctly-use-the-function-subs#answer_1489681

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2139761-how-to-correctly-use-the-function-subs#answer_1489681

Open in MATLAB Online

Hi Tony,

I tried reproducing the issue you are facing -

syms B

A = sym('A', [6 1]);

C = sym('C', [2 1]);

A_val = [1, 2, 3, 4, 5, 6];

B_val = 7;

C_val = [8, 9];

Jacobian_mtx_1 = [A(1)*B + C(1), A(2), A(3);

A(4), A(5)*B + C(2), A(6);

B, C(1), C(2)];

disp(Jacobian_mtx_1);

how to correctly use the function "subs" (6)

Jacobian_mtx_1 = subs( Jacobian_mtx_1 , { A , B , C } , { A_val ,B_val , C_val } ) ;

Error using sym/subs (line 156)
Entries in second argument must be scalar.

Instead of directly replacing A and C with vectors, I tried replacing the elements one by one and it wokred for me -

syms B

A = sym('A', [6 1]);

C = sym('C', [2 1]);

A_val = [1, 2, 3, 4, 5, 6];

B_val = 7;

C_val = [8, 9];

Jacobian_mtx_1 = [A(1)*B + C(1), A(2), A(3);

A(4), A(5)*B + C(2), A(6);

B, C(1), C(2)];

disp(Jacobian_mtx_1);

for i = 1:length(A)

Jacobian_mtx_1 = subs(Jacobian_mtx_1, A(i), A_val(i));

end

for i = 1:length(C)

Jacobian_mtx_1 = subs(Jacobian_mtx_1, C(i), C_val(i));

end

% Now replace B

Jacobian_mtx_1 = subs(Jacobian_mtx_1, B, B_val);

disp(Jacobian_mtx_1);

1 Comment

Show -1 older commentsHide -1 older comments

Tony Cheng on 24 Jul 2024 at 6:47

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2139761-how-to-correctly-use-the-function-subs#comment_3219251

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2139761-how-to-correctly-use-the-function-subs#comment_3219251

Hi Kumar,

Many thanks to your reply! I think your way is feasible, but the following manner is a little bit easier

Jacobian_mtx_1 = subs( Jacobian_mtx_1 , A , A_all ) ;

Jacobian_mtx_1 = subs( Jacobian_mtx_1 , B , B_val ) ;

Jacobian_mtx_1 = subs( Jacobian_mtx_1 , C , C_val) ;

cause on

it says If old and new are both vectors or cell arrays of the same size, subs replaces each element of old with the corresponding element of new.

Then we can use vector replacements directly.

Cheers

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

Mathematics and OptimizationSymbolic Math Toolbox

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Tags

  • subs

Products

  • MATLAB

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


how to correctly use the function "subs" (8)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

how to correctly use the function "subs" (2024)

References

Top Articles
Bush Hill Frenchies
Characters Bio's - Shannon Messenger
Spasa Parish
Rentals for rent in Maastricht
159R Bus Schedule Pdf
Sallisaw Bin Store
Black Adam Showtimes Near Maya Cinemas Delano
Espn Transfer Portal Basketball
Pollen Levels Richmond
11 Best Sites Like The Chive For Funny Pictures and Memes
Things to do in Wichita Falls on weekends 12-15 September
Craigslist Pets Huntsville Alabama
Paulette Goddard | American Actress, Modern Times, Charlie Chaplin
What's the Difference Between Halal and Haram Meat & Food?
R/Skinwalker
Rugged Gentleman Barber Shop Martinsburg Wv
Jennifer Lenzini Leaving Ktiv
Justified - Streams, Episodenguide und News zur Serie
Epay. Medstarhealth.org
Olde Kegg Bar & Grill Portage Menu
Cubilabras
Half Inning In Which The Home Team Bats Crossword
Amazing Lash Bay Colony
Juego Friv Poki
Dirt Devil Ud70181 Parts Diagram
Truist Bank Open Saturday
Water Leaks in Your Car When It Rains? Common Causes & Fixes
What’s Closing at Disney World? A Complete Guide
New from Simply So Good - Cherry Apricot Slab Pie
Drys Pharmacy
Ohio State Football Wiki
Find Words Containing Specific Letters | WordFinder®
FirstLight Power to Acquire Leading Canadian Renewable Operator and Developer Hydromega Services Inc. - FirstLight
Webmail.unt.edu
2024-25 ITH Season Preview: USC Trojans
Metro By T Mobile Sign In
Restored Republic December 1 2022
12 30 Pacific Time
Free Stuff Craigslist Roanoke Va
Stellaris Resolution
Wi Dept Of Regulation & Licensing
Pick N Pull Near Me [Locator Map + Guide + FAQ]
Crystal Westbrooks Nipple
Ice Hockey Dboard
Über 60 Prozent Rabatt auf E-Bikes: Aldi reduziert sämtliche Pedelecs stark im Preis - nur noch für kurze Zeit
Wie blocke ich einen Bot aus Boardman/USA - sellerforum.de
Infinity Pool Showtimes Near Maya Cinemas Bakersfield
Dermpathdiagnostics Com Pay Invoice
How To Use Price Chopper Points At Quiktrip
Maria Butina Bikini
Busted Newspaper Zapata Tx
Latest Posts
Article information

Author: Terrell Hackett

Last Updated:

Views: 5609

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Terrell Hackett

Birthday: 1992-03-17

Address: Suite 453 459 Gibson Squares, East Adriane, AK 71925-5692

Phone: +21811810803470

Job: Chief Representative

Hobby: Board games, Rock climbing, Ghost hunting, Origami, Kabaddi, Mushroom hunting, Gaming

Introduction: My name is Terrell Hackett, I am a gleaming, brainy, courageous, helpful, healthy, cooperative, graceful person who loves writing and wants to share my knowledge and understanding with you.